<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    

<head><meta name="generator" content="Hexo 3.8.0">
    <meta charset="utf-8">
    <meta name="keywords" content="Struts2 总结, Zhou•Yong">
    <meta name="description" content="简介
三层架构：展示层  业务逻辑层   数据持久层
struts2框架:展示层框架，mvc框架
apache公司
并不是struts1的升级版，是webwork升级
核心：核心控制器  拦截器

配置环境搭建1.直接下载导入http://">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>Struts2 总结 | Zhou•Yong</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/css/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery-2.2.0.min.js"></script>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>

</head>

<body>

<header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="container">
            <div class="nav-wrapper">
                <div class="brand-logo">
                    <a href="/" class="waves-effect waves-light">
                        
                        <img src="/medias/logo.png" class="logo-img hide-on-small-only">
                        
                        <span class="logo-span">Zhou•Yong</span>
                    </a>
                </div>
                <a href="#" data-activates="mobile-nav" class="button-collapse"><i class="fa fa-navicon"></i></a>
<ul class="right">
    
    <li class="hide-on-med-and-down">
        <a href="/" class="waves-effect waves-light">
            
            <i class="fa fa-home"></i>
            
            <span>主页</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/about" class="waves-effect waves-light">
            
            <i class="fa fa-user-circle-o"></i>
            
            <span>个人介绍</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/categories" class="waves-effect waves-light">
            
            <i class="fa fa-bookmark"></i>
            
            <span>分类</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/tags" class="waves-effect waves-light">
            
            <i class="fa fa-tags"></i>
            
            <span>标签</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/friends" class="waves-effect waves-light">
            
            <i class="fa fa-address-book"></i>
            
            <span>推荐</span>
        </a>
    </li>
    
    <li>
        <a id="toggleSearch" class="waves-effect waves-light">
            <i id="searchIcon" class="mdi-action-search"></i>
        </a>
    </li>

</ul>

<div class="side-nav" id="mobile-nav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">Zhou•Yong</div>
        <div class="logo-desc">
            
            你迈出的步伐不非得很大，只要你是在正确的方向前进就行
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li>
            <a href="/" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-home"></i>
                
                主页
            </a>
        </li>
        
        <li>
            <a href="/about" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-user-circle-o"></i>
                
                个人介绍
            </a>
        </li>
        
        <li>
            <a href="/categories" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-bookmark"></i>
                
                分类
            </a>
        </li>
        
        <li>
            <a href="/tags" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-tags"></i>
                
                标签
            </a>
        </li>
        
        <li>
            <a href="/friends" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-address-book"></i>
                
                推荐
            </a>
        </li>
        
        
    </ul>

    <div class="social-link"><a href="#!" class="tooltipped" data-tooltip="邮件联系我: zy20081@126.com" data-position="top" data-delay="50">
    <i class="fa fa-envelope-open"></i>
</a>
<a href="#!" class="tooltipped" data-tooltip="QQ联系我: 314153271" data-position="top" data-delay="50">
    <i class="fa fa-qq"></i>
</a>

<a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
    <i class="fa fa-rss"></i>
</a>
</div>
</div>

            </div>
        </div>

        
    </nav>
</header>





<div class="bg-cover post-cover" style="background-image: url('/medias/featureimages/7.jpg')">
    <div class="container">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <div class="description center-align post-title">
                        Struts2 总结
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1,
    #articleContent h2,
    #articleContent h3,
    #articleContent h4,
    #articleContent h5,
    #articleContent h6 {
        padding-top: 76px;
        margin-top: -76px;
    }

    #articleContent h1 {
        line-height: 3.5rem;
    }

    #articleContent h2 {
        line-height: 3.2rem;
    }

    #articleContent h3 {
        line-height: 2.8rem;
    }

    #articleContent h4 {
        line-height: 2.5rem;
    }

    #articleContent h5 {
        line-height: 2.2rem;
    }

    #articleContent h6 {
        line-height: 1.9rem;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }
</style>
<div class="row">
    <div class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            
            <div class="article-tag">
                
                <a href="/tags/框架/" target="_blank">
                    <span class="chip bg-color">框架</span>
                </a>
                
                <a href="/tags/SSH/" target="_blank">
                    <span class="chip bg-color">SSH</span>
                </a>
                
            </div>
            
            <div class="post-info">
                
                <span class="post-cate">
                    <i class="fa fa-bookmark fa-fw icon-category"></i>
                    
                    <a href="/categories/JAVA/" class="post-category" target="_blank">
                        JAVA
                    </a>
                    
                </span>
                

                <span class="post-date">
                    <i class="fa fa-clock-o fa-fw"></i>2018-11-25
                </span>
            </div>
        </div>
        <hr>
        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><ol>
<li>三层架构：展示层  业务逻辑层   数据持久层</li>
<li>struts2框架:展示层框架，mvc框架</li>
<li>apache公司</li>
<li>并不是struts1的升级版，是webwork升级</li>
<li>核心：核心控制器  拦截器</li>
</ol>
<h2 id="配置环境搭建"><a href="#配置环境搭建" class="headerlink" title="配置环境搭建"></a>配置环境搭建</h2><p>1.直接下载导入<br><a href="http://struts.apache.org/download.cgi" target="_blank" rel="noopener">http://struts.apache.org/download.cgi</a>,<br>可根据需要选择 all min src 等包，如果仅仅是学习，选择 min 包就够了</p>
<pre class=" language-bash"><code class="language-bash"> web.xml
<span class="token operator">&lt;</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"UTF-8"</span>?<span class="token operator">></span>
<span class="token operator">&lt;</span>web-app xmlns:xsi<span class="token operator">=</span><span class="token string">"http://www.w3.org/2001/XMLSchema-instance"</span> xmlns<span class="token operator">=</span><span class="token string">"http://java.sun.com/xml/ns/javaee"</span> xsi:schemaLocation<span class="token operator">=</span><span class="token string">"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</span> id<span class="token operator">=</span><span class="token string">"WebApp_ID"</span> version<span class="token operator">=</span><span class="token string">"2.5"</span><span class="token operator">></span>
  <span class="token operator">&lt;</span>display-name<span class="token operator">></span>struts2_day01<span class="token operator">&lt;</span>/display-name<span class="token operator">></span>
  <span class="token operator">&lt;</span><span class="token operator">!</span>-- 配置常量 
  <span class="token operator">&lt;</span>context-param<span class="token operator">></span>
      <span class="token operator">&lt;</span>param-name<span class="token operator">></span>struts.i18n.encoding<span class="token operator">&lt;</span>/param-name<span class="token operator">></span>
      <span class="token operator">&lt;</span>param-value<span class="token operator">></span>UTF-8<span class="token operator">&lt;</span>/param-value<span class="token operator">></span>
  <span class="token operator">&lt;</span>/context-param<span class="token operator">></span>
  --<span class="token operator">></span>
  <span class="token operator">&lt;</span><span class="token operator">!</span>-- struts2核心过滤器 --<span class="token operator">></span>
  <span class="token operator">&lt;</span>filter<span class="token operator">></span>
      <span class="token operator">&lt;</span>filter-name<span class="token operator">></span>struts2<span class="token operator">&lt;</span>/filter-name<span class="token operator">></span>
      <span class="token operator">&lt;</span>filter-class<span class="token operator">></span>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter<span class="token operator">&lt;</span>/filter-class<span class="token operator">></span>
  <span class="token operator">&lt;</span>/filter<span class="token operator">></span>
  <span class="token operator">&lt;</span>filter-mapping<span class="token operator">></span>
      <span class="token operator">&lt;</span>filter-name<span class="token operator">></span>struts2<span class="token operator">&lt;</span>/filter-name<span class="token operator">></span>
      <span class="token operator">&lt;</span>url-pattern<span class="token operator">></span>/*<span class="token operator">&lt;</span>/url-pattern<span class="token operator">></span>
  <span class="token operator">&lt;</span>/filter-mapping<span class="token operator">></span>

  <span class="token operator">&lt;</span>welcome-file-list<span class="token operator">></span>
    <span class="token operator">&lt;</span>welcome-file<span class="token operator">></span>index.html<span class="token operator">&lt;</span>/welcome-file<span class="token operator">></span>
    <span class="token operator">&lt;</span>welcome-file<span class="token operator">></span>index.htm<span class="token operator">&lt;</span>/welcome-file<span class="token operator">></span>
    <span class="token operator">&lt;</span>welcome-file<span class="token operator">></span>index.jsp<span class="token operator">&lt;</span>/welcome-file<span class="token operator">></span>
    <span class="token operator">&lt;</span>welcome-file<span class="token operator">></span>default.html<span class="token operator">&lt;</span>/welcome-file<span class="token operator">></span>
    <span class="token operator">&lt;</span>welcome-file<span class="token operator">></span>default.htm<span class="token operator">&lt;</span>/welcome-file<span class="token operator">></span>
    <span class="token operator">&lt;</span>welcome-file<span class="token operator">></span>default.jsp<span class="token operator">&lt;</span>/welcome-file<span class="token operator">></span>
  <span class="token operator">&lt;</span>/welcome-file-list<span class="token operator">></span>
<span class="token operator">&lt;</span>/web-app<span class="token operator">></span>
</code></pre>
<pre class=" language-bash"><code class="language-bash">struts.xml
<span class="token operator">&lt;</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"UTF-8"</span>?<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>DOCTYPE struts PUBLIC
    <span class="token string">"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"</span>
    <span class="token string">"http://struts.apache.org/dtds/struts-2.3.dtd"</span><span class="token operator">></span>
<span class="token operator">&lt;</span>struts<span class="token operator">></span>

    <span class="token operator">&lt;</span><span class="token operator">!</span>-- i18n:国际化. 解决post提交乱码 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>constant name<span class="token operator">=</span><span class="token string">"struts.i18n.encoding"</span> value<span class="token operator">=</span><span class="token string">"UTF-8"</span><span class="token operator">></span><span class="token operator">&lt;</span>/constant<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 指定反问action时的后缀名 
        http://localhost:8080/struts2_day01/hello/HelloAction.do
    --<span class="token operator">></span>
    <span class="token operator">&lt;</span>constant name<span class="token operator">=</span><span class="token string">"struts.action.extension"</span> value<span class="token operator">=</span><span class="token string">"action"</span><span class="token operator">></span><span class="token operator">&lt;</span>/constant<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 指定struts2是否以开发模式运行
            1.热加载主配置.<span class="token punctuation">(</span>不需要重启即可生效<span class="token punctuation">)</span>
            2.提供更多错误信息输出,方便开发时的调试
     --<span class="token operator">></span>
    <span class="token operator">&lt;</span>constant name<span class="token operator">=</span><span class="token string">"struts.devMode"</span> value<span class="token operator">=</span><span class="token string">"true"</span><span class="token operator">></span><span class="token operator">&lt;</span>/constant<span class="token operator">></span>

    <span class="token operator">&lt;</span><span class="token operator">!</span>-- package:将Action配置封装.就是可以在Package中配置很多action.
            name属性: 给包起个名字,起到标识作用.随便起.不能其他包名重复.
            namespace属性:给action的访问路径中定义一个命名空间
            extends属性: 继承一个 指定包
            abstract属性:包是否为抽象的<span class="token punctuation">;</span> 标识性属性.标识该包不能独立运行.专门被继承
      --<span class="token operator">></span>
    <span class="token operator">&lt;</span>package name<span class="token operator">=</span><span class="token string">"hello"</span> namespace<span class="token operator">=</span><span class="token string">"/hello"</span> extends<span class="token operator">=</span><span class="token string">"struts-default"</span> <span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">!</span>-- action元素:配置action类
                name属性: 决定了Action访问资源名.
                class属性: action的完整类名
                method属性: 指定调用Action中的哪个方法来处理请求
         --<span class="token operator">></span>
        <span class="token operator">&lt;</span>action name<span class="token operator">=</span><span class="token string">"HelloAction"</span> class<span class="token operator">=</span><span class="token string">"cn.itheima.a_hello.HelloAction"</span> method<span class="token operator">=</span><span class="token string">"hello"</span> <span class="token operator">></span>
            <span class="token operator">&lt;</span><span class="token operator">!</span>-- result元素:结果配置 
                    name属性: 标识结果处理的名称.与action方法的返回值对应.
                    type属性: 指定调用哪一个result类来处理结果,默认使用转发.
                    标签体:填写页面的相对路径
            --<span class="token operator">></span>
            <span class="token operator">&lt;</span>result name<span class="token operator">=</span><span class="token string">"success"</span> type<span class="token operator">=</span><span class="token string">"dispatcher"</span> <span class="token operator">></span>/hello.jsp<span class="token operator">&lt;</span>/result<span class="token operator">></span>
        <span class="token operator">&lt;</span>/action<span class="token operator">></span>
    <span class="token operator">&lt;</span>/package<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 引入其他struts配置文件 --<span class="token operator">></span>
    <span class="token operator">&lt;</span>include file<span class="token operator">=</span><span class="token string">"cn/itheima/b_dynamic/struts.xml"</span><span class="token operator">></span><span class="token operator">&lt;</span>/include<span class="token operator">></span>
    <span class="token operator">&lt;</span>include file<span class="token operator">=</span><span class="token string">"cn/itheima/c_default/struts.xml"</span><span class="token operator">></span><span class="token operator">&lt;</span>/include<span class="token operator">></span>
<span class="token operator">&lt;</span>/struts<span class="token operator">></span>
</code></pre>
<pre class=" language-bash"><code class="language-bash"> public class HelloAction <span class="token punctuation">{</span>

    public String hello<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>

        System.out.println<span class="token punctuation">(</span><span class="token string">"hello world!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">return</span> <span class="token string">"success"</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre>
<h2 id="获取-Servlet-Api"><a href="#获取-Servlet-Api" class="headerlink" title="获取 Servlet Api"></a>获取 Servlet Api</h2><pre class=" language-bash"><code class="language-bash"> struts.xml
<span class="token operator">&lt;</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"UTF-8"</span>?<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>DOCTYPE struts PUBLIC
    <span class="token string">"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"</span>
    <span class="token string">"http://struts.apache.org/dtds/struts-2.3.dtd"</span><span class="token operator">></span>
<span class="token operator">&lt;</span>struts<span class="token operator">></span>
    <span class="token operator">&lt;</span>package name<span class="token operator">=</span><span class="token string">"result"</span> namespace<span class="token operator">=</span><span class="token string">"/"</span> extends<span class="token operator">=</span><span class="token string">"struts-default"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span>--  转发 --<span class="token operator">></span>
        <span class="token operator">&lt;</span>action name<span class="token operator">=</span><span class="token string">"Demo1Action"</span> class<span class="token operator">=</span><span class="token string">"cn.itheima.a_result.Demo1Action"</span> method<span class="token operator">=</span><span class="token string">"execute"</span> <span class="token operator">></span>
            <span class="token operator">&lt;</span>result name<span class="token operator">=</span><span class="token string">"success"</span> type<span class="token operator">=</span><span class="token string">"dispatcher"</span> <span class="token operator">></span>/hello.jsp<span class="token operator">&lt;</span>/result<span class="token operator">></span>
        <span class="token operator">&lt;</span>/action<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 重定向 --<span class="token operator">></span>
        <span class="token operator">&lt;</span>action name<span class="token operator">=</span><span class="token string">"Demo2Action"</span> class<span class="token operator">=</span><span class="token string">"cn.itheima.a_result.Demo2Action"</span> method<span class="token operator">=</span><span class="token string">"execute"</span> <span class="token operator">></span>
            <span class="token operator">&lt;</span>result name<span class="token operator">=</span><span class="token string">"success"</span> type<span class="token operator">=</span><span class="token string">"redirect"</span> <span class="token operator">></span>/hello.jsp<span class="token operator">&lt;</span>/result<span class="token operator">></span>
        <span class="token operator">&lt;</span>/action<span class="token operator">></span>
    <span class="token operator">&lt;</span><span class="token operator">!</span>-- 转发到Action --<span class="token operator">></span>
        <span class="token operator">&lt;</span>action name<span class="token operator">=</span><span class="token string">"Demo3Action"</span> class<span class="token operator">=</span><span class="token string">"cn.itheima.a_result.Demo3Action"</span> method<span class="token operator">=</span><span class="token string">"execute"</span> <span class="token operator">></span>
             <span class="token operator">&lt;</span>result name<span class="token operator">=</span><span class="token string">"success"</span> type<span class="token operator">=</span><span class="token string">"chain"</span><span class="token operator">></span>
                     <span class="token operator">&lt;</span><span class="token operator">!</span>-- action的名字 --<span class="token operator">></span>
                 <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"actionName"</span><span class="token operator">></span>Demo1Action<span class="token operator">&lt;</span>/param<span class="token operator">></span>
                     <span class="token operator">&lt;</span><span class="token operator">!</span>-- action所在的命名空间 --<span class="token operator">></span>
                 <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"namespace"</span><span class="token operator">></span>/<span class="token operator">&lt;</span>/param<span class="token operator">></span>
             <span class="token operator">&lt;</span>/result<span class="token operator">></span>
        <span class="token operator">&lt;</span>/action<span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">!</span>-- 重定向到Action --<span class="token operator">></span>
        <span class="token operator">&lt;</span>action name<span class="token operator">=</span><span class="token string">"Demo4Action"</span> class<span class="token operator">=</span><span class="token string">"cn.itheima.a_result.Demo4Action"</span> method<span class="token operator">=</span><span class="token string">"execute"</span> <span class="token operator">></span>
            <span class="token operator">&lt;</span>result  name<span class="token operator">=</span><span class="token string">"success"</span>  type<span class="token operator">=</span><span class="token string">"redirectAction"</span><span class="token operator">></span>
                 <span class="token operator">&lt;</span><span class="token operator">!</span>-- action的名字 --<span class="token operator">></span>
                 <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"actionName"</span><span class="token operator">></span>Demo1Action<span class="token operator">&lt;</span>/param<span class="token operator">></span>
                 <span class="token operator">&lt;</span><span class="token operator">!</span>-- action所在的命名空间 --<span class="token operator">></span>
                 <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"namespace"</span><span class="token operator">></span>/<span class="token operator">&lt;</span>/param<span class="token operator">></span>
            <span class="token operator">&lt;</span>/result<span class="token operator">></span>
        <span class="token operator">&lt;</span>/action<span class="token operator">></span>
    <span class="token operator">&lt;</span>/package<span class="token operator">></span>
    <span class="token operator">&lt;</span>include file<span class="token operator">=</span><span class="token string">"cn/itheima/b_api/struts.xml"</span><span class="token operator">></span><span class="token operator">&lt;</span>/include<span class="token operator">></span>
    <span class="token operator">&lt;</span>include file<span class="token operator">=</span><span class="token string">"cn/itheima/c_param/struts.xml"</span><span class="token operator">></span><span class="token operator">&lt;</span>/include<span class="token operator">></span>
<span class="token operator">&lt;</span>/struts<span class="token operator">></span>
</code></pre>
<pre class=" language-bash"><code class="language-bash">  //如何在action中获得原生ServletAPI
public class Demo5Action extends ActionSupport <span class="token punctuation">{</span>

    public String execute<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception <span class="token punctuation">{</span>
        //request域<span class="token operator">=</span><span class="token operator">></span> map <span class="token punctuation">(</span>struts2并不推荐使用原生request域<span class="token punctuation">)</span>
        //不推荐
        Map<span class="token operator">&lt;</span>String, Object<span class="token operator">></span> requestScope <span class="token operator">=</span> <span class="token punctuation">(</span>Map<span class="token operator">&lt;</span>String, Object<span class="token operator">></span><span class="token punctuation">)</span> ActionContext.getContext<span class="token punctuation">(</span><span class="token punctuation">)</span>.get<span class="token punctuation">(</span><span class="token string">"request"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        //推荐
        ActionContext.getContext<span class="token punctuation">(</span><span class="token punctuation">)</span>.put<span class="token punctuation">(</span><span class="token string">"name"</span>, <span class="token string">"requestTom"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        //session域 <span class="token operator">=</span><span class="token operator">></span> map
        Map<span class="token operator">&lt;</span>String, Object<span class="token operator">></span> sessionScope <span class="token operator">=</span> ActionContext.getContext<span class="token punctuation">(</span><span class="token punctuation">)</span>.getSession<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        sessionScope.put<span class="token punctuation">(</span><span class="token string">"name"</span>, <span class="token string">"sessionTom"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        //application域<span class="token operator">=</span><span class="token operator">></span>map
        Map<span class="token operator">&lt;</span>String, Object<span class="token operator">></span> applicationScope <span class="token operator">=</span> ActionContext.getContext<span class="token punctuation">(</span><span class="token punctuation">)</span>.getApplication<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        applicationScope.put<span class="token punctuation">(</span><span class="token string">"name"</span>, <span class="token string">"applicationTom"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">return</span> SUCCESS<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
//如何在action中获得原生ServletAPI
public class Demo6Action extends ActionSupport <span class="token punctuation">{</span>
    //并不推荐
    public String execute<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception <span class="token punctuation">{</span>
        //原生request
        HttpServletRequest request <span class="token operator">=</span> ServletActionContext.getRequest<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        //原生session
        HttpSession session <span class="token operator">=</span> request.getSession<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        //原生response
        HttpServletResponse response <span class="token operator">=</span> ServletActionContext.getResponse<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        //原生servletContext
        ServletContext servletContext <span class="token operator">=</span> ServletActionContext.getServletContext<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> SUCCESS<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
//如何在action中获得原生ServletAPI
public class Demo7Action extends ActionSupport implements ServletRequestAware <span class="token punctuation">{</span>

    private HttpServletRequest request<span class="token punctuation">;</span>
    public String execute<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception <span class="token punctuation">{</span> 

        System.out.println<span class="token punctuation">(</span><span class="token string">"原生request:"</span>+request<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> SUCCESS<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    @Override
    public void setServletRequest<span class="token punctuation">(</span>HttpServletRequest request<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        this.request <span class="token operator">=</span> request<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="传参"><a href="#传参" class="headerlink" title="传参"></a>传参</h2><p>在传统的servlet中我们采用request和response传递参数，但是struts2如果想用同样的方式传递参数，必须先：</p>
<pre class=" language-bash"><code class="language-bash">HttpServletRequest req <span class="token operator">=</span> ServletActionContext.getRequest<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
HttpServletResponse resp <span class="token operator">=</span> ServletActionContext.getResponse<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>获取request和response</p>
<p>除了以上这种方式，struts2还带了3中传参方式：</p>
<ol>
<li>首先是最简单的get/set方式：只要在继承了ActionSupport父类的struts2类中定义私有成员变量（变量名和jsp页面中上传参数的name相同），并生成get/set方法，即可直接使用。</li>
</ol>
<pre class=" language-bash"><code class="language-bash">public class LoginAction extends ActionSupport<span class="token punctuation">{</span>

private String username<span class="token punctuation">;</span>

public String Login<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>

System.out.println<span class="token punctuation">(</span><span class="token string">"loginaction:"</span>+username<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token punctuation">}</span>

public String getUsername<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> username<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
public void setUsername<span class="token punctuation">(</span>String username<span class="token punctuation">)</span> 
<span class="token punctuation">{</span>
 this.username <span class="token operator">=</span> username<span class="token punctuation">;</span>
 <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<pre class=" language-bash"><code class="language-bash">jsp的form中：

<span class="token operator">&lt;</span>form action<span class="token operator">=</span><span class="token string">"login"</span> method<span class="token operator">=</span><span class="token string">"POST"</span> id<span class="token operator">=</span><span class="token string">"form"</span><span class="token operator">></span>
<span class="token operator">&lt;</span>div class<span class="token operator">=</span><span class="token string">"inputText"</span><span class="token operator">></span><span class="token operator">&lt;</span>span<span class="token operator">></span>用户名： 
<span class="token operator">&lt;</span>/span<span class="token operator">></span><span class="token operator">&lt;</span>input type<span class="token operator">=</span><span class="token string">"text"</span> name<span class="token operator">=</span><span class="token string">"username"</span> id<span class="token operator">=</span><span class="token string">"username"</span> class<span class="token operator">=</span><span class="token string">"username"</span><span class="token operator">></span><span class="token operator">&lt;</span>/div<span class="token operator">></span>
<span class="token operator">&lt;</span>/form<span class="token operator">></span>
</code></pre>
<ol start="2">
<li><p>在java中我们一般会设置bean类，比如这里：</p>
<pre class=" language-bash"><code class="language-bash">public class User
<span class="token punctuation">{</span>

private String username<span class="token punctuation">;</span>
public String getUsername<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> username<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
public void setUsername<span class="token punctuation">(</span>String username<span class="token punctuation">)</span> <span class="token punctuation">{</span>
this.username <span class="token operator">=</span> username<span class="token punctuation">;</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>那么我们再传参的时候也可以直接传递User类</p>
</li>
</ol>
<p>但是为了分清楚jsp传递过去的变量时给哪个类的（加入我们的Company类和User类中都有username变量），我们在jsp页面需要进行改动，在name的值中加上前缀（是在action类中声明的类变量），</p>
<pre class=" language-bash"><code class="language-bash">public class LoginAction extends ActionSupport<span class="token punctuation">{</span>

private User user<span class="token punctuation">;</span>  //不需要实例化，只需要声明

public String Login<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>

System.out.println<span class="token punctuation">(</span><span class="token string">"loginaction:"</span>+user.username<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token punctuation">}</span>

public String getUser<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>  //也需要有get/set方法
<span class="token keyword">return</span> user<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
public void setUser<span class="token punctuation">(</span>String user<span class="token punctuation">)</span> <span class="token punctuation">{</span>
this.user <span class="token operator">=</span> user<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<pre class=" language-bash"><code class="language-bash">jsp页面只改了name的值，注意是user不是User

<span class="token operator">&lt;</span>form action<span class="token operator">=</span><span class="token string">"login"</span> method<span class="token operator">=</span><span class="token string">"POST"</span> id<span class="token operator">=</span><span class="token string">"form"</span><span class="token operator">></span>
<span class="token operator">&lt;</span>div class<span class="token operator">=</span><span class="token string">"inputText"</span><span class="token operator">></span><span class="token operator">&lt;</span>span<span class="token operator">></span>用户名： <span class="token operator">&lt;</span>/span<span class="token operator">></span><span class="token operator">&lt;</span>input type<span class="token operator">=</span><span class="token string">"text"</span> name<span class="token operator">=</span><span class="token string">"user.username"</span> id<span class="token operator">=</span><span class="token string">"username"</span> class<span class="token operator">=</span><span class="token string">"username"</span><span class="token operator">></span><span class="token operator">&lt;</span>/div<span class="token operator">></span>
<span class="token operator">&lt;</span>/form<span class="token operator">></span>
</code></pre>
<ol start="3">
<li>比较推荐的第三种方法，后端和前端耦合较少。<br>这种方法action类需要继承ModelDriven<t>接口。<pre class=" language-bash"><code class="language-bash">public class LoginAction extends ActionSupport implements ModelDriven<span class="token operator">&lt;</span>User<span class="token operator">></span>
 <span class="token punctuation">{</span> //这里需要把User加上去
 private User user<span class="token operator">=</span>new User<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> //和2中不同，需要实例化User
 public String Login<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
 System.out.println<span class="token punctuation">(</span><span class="token string">"loginaction:"</span>+user.username<span class="token punctuation">)</span><span class="token punctuation">;</span>
 <span class="token punctuation">}</span>
 public User getModel<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
 //不再需要get/set方法，而是实现ModelDriven接口中的getMode方法，返回值是user。
 // TODO Auto-generated method stub
 <span class="token keyword">return</span> user<span class="token punctuation">;</span>
 <span class="token punctuation">}</span>
 <span class="token punctuation">}</span>
</code></pre>
</t></li>
</ol>
<h2 id="OGNL表达式"><a href="#OGNL表达式" class="headerlink" title="OGNL表达式"></a>OGNL表达式</h2><p> <img src="/2018/11/25/struts2-zong-jie/1.jpg" alt="图1"></p>
<pre class=" language-bash"><code class="language-bash">package cn.a_ognl<span class="token punctuation">;</span>
<span class="token function">import</span> java.util.HashMap<span class="token punctuation">;</span>
<span class="token function">import</span> java.util.Map<span class="token punctuation">;</span>
<span class="token function">import</span> org.junit.Test<span class="token punctuation">;</span>
<span class="token function">import</span> cn.bean.User<span class="token punctuation">;</span>
<span class="token function">import</span> ognl.Ognl<span class="token punctuation">;</span>
<span class="token function">import</span> ognl.OgnlContext<span class="token punctuation">;</span>
<span class="token function">import</span> ognl.OgnlException<span class="token punctuation">;</span>

//展示OGNL语法
public class Demo <span class="token punctuation">{</span>
    @Test
    //准备工作
    public void fun1<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception<span class="token punctuation">{</span>
        //准备ONGLContext
            //准备Root
            User rootUser <span class="token operator">=</span> new User<span class="token punctuation">(</span><span class="token string">"tom"</span>,18<span class="token punctuation">)</span><span class="token punctuation">;</span>
            //准备Context
            Map<span class="token operator">&lt;</span>String,User<span class="token operator">></span> context <span class="token operator">=</span> new HashMap<span class="token operator">&lt;</span>String,User<span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            context.put<span class="token punctuation">(</span><span class="token string">"user1"</span>, new User<span class="token punctuation">(</span><span class="token string">"jack"</span>,18<span class="token punctuation">))</span><span class="token punctuation">;</span>
            context.put<span class="token punctuation">(</span><span class="token string">"user2"</span>, new User<span class="token punctuation">(</span><span class="token string">"rose"</span>,22<span class="token punctuation">))</span><span class="token punctuation">;</span>
        OgnlContext oc <span class="token operator">=</span> new OgnlContext<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        //将rootUser作为root部分
        oc.setRoot<span class="token punctuation">(</span>rootUser<span class="token punctuation">)</span><span class="token punctuation">;</span>
        //将context这个Map作为Context部分
        oc.setValues<span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">;</span>
        //书写OGNL
        Ognl.getValue<span class="token punctuation">(</span><span class="token string">""</span>, oc, oc.getRoot<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    @Test
    //基本语法演示
    //取出root中的属性值
    public void fun2<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception<span class="token punctuation">{</span>
        //准备ONGLContext
            //准备Root
            User rootUser <span class="token operator">=</span> new User<span class="token punctuation">(</span><span class="token string">"tom"</span>,18<span class="token punctuation">)</span><span class="token punctuation">;</span>
            //准备Context
            Map<span class="token operator">&lt;</span>String,User<span class="token operator">></span> context <span class="token operator">=</span> new HashMap<span class="token operator">&lt;</span>String,User<span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            context.put<span class="token punctuation">(</span><span class="token string">"user1"</span>, new User<span class="token punctuation">(</span><span class="token string">"jack"</span>,18<span class="token punctuation">))</span><span class="token punctuation">;</span>
            context.put<span class="token punctuation">(</span><span class="token string">"user2"</span>, new User<span class="token punctuation">(</span><span class="token string">"rose"</span>,22<span class="token punctuation">))</span><span class="token punctuation">;</span>
        OgnlContext oc <span class="token operator">=</span> new OgnlContext<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        oc.setRoot<span class="token punctuation">(</span>rootUser<span class="token punctuation">)</span><span class="token punctuation">;</span>
        oc.setValues<span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">;</span>
        //书写OGNL

        //取出root中user对象的name属性
        String name <span class="token operator">=</span> <span class="token punctuation">(</span>String<span class="token punctuation">)</span> Ognl.getValue<span class="token punctuation">(</span><span class="token string">"name"</span>, oc, oc.getRoot<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
        Integer age <span class="token operator">=</span> <span class="token punctuation">(</span>Integer<span class="token punctuation">)</span> Ognl.getValue<span class="token punctuation">(</span><span class="token string">"age"</span>, oc, oc.getRoot<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
        System.out.println<span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        System.out.println<span class="token punctuation">(</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    @Test
    //基本语法演示
    //取出context中的属性值
    public void fun3<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception<span class="token punctuation">{</span>
        //准备ONGLContext
            //准备Root
            User rootUser <span class="token operator">=</span> new User<span class="token punctuation">(</span><span class="token string">"tom"</span>,18<span class="token punctuation">)</span><span class="token punctuation">;</span>
            //准备Context
            Map<span class="token operator">&lt;</span>String,User<span class="token operator">></span> context <span class="token operator">=</span> new HashMap<span class="token operator">&lt;</span>String,User<span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            context.put<span class="token punctuation">(</span><span class="token string">"user1"</span>, new User<span class="token punctuation">(</span><span class="token string">"jack"</span>,18<span class="token punctuation">))</span><span class="token punctuation">;</span>
            context.put<span class="token punctuation">(</span><span class="token string">"user2"</span>, new User<span class="token punctuation">(</span><span class="token string">"rose"</span>,22<span class="token punctuation">))</span><span class="token punctuation">;</span>
        OgnlContext oc <span class="token operator">=</span> new OgnlContext<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        oc.setRoot<span class="token punctuation">(</span>rootUser<span class="token punctuation">)</span><span class="token punctuation">;</span>
        oc.setValues<span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">;</span>
        //书写OGNL

        //取出context中键为user1对象的name属性
        String name <span class="token operator">=</span> <span class="token punctuation">(</span>String<span class="token punctuation">)</span> Ognl.getValue<span class="token punctuation">(</span><span class="token string">"#user1.name"</span>, oc, oc.getRoot<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
        String name2 <span class="token operator">=</span> <span class="token punctuation">(</span>String<span class="token punctuation">)</span> Ognl.getValue<span class="token punctuation">(</span><span class="token string">"#user2.name"</span>, oc, oc.getRoot<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
        Integer age <span class="token operator">=</span> <span class="token punctuation">(</span>Integer<span class="token punctuation">)</span> Ognl.getValue<span class="token punctuation">(</span><span class="token string">"#user2.age"</span>, oc, oc.getRoot<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
        System.out.println<span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        System.out.println<span class="token punctuation">(</span>name2<span class="token punctuation">)</span><span class="token punctuation">;</span>
        System.out.println<span class="token punctuation">(</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    @Test
    //基本语法演示
    //为属性赋值
    public void fun4<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception<span class="token punctuation">{</span>
        //准备ONGLContext
            //准备Root
            User rootUser <span class="token operator">=</span> new User<span class="token punctuation">(</span><span class="token string">"tom"</span>,18<span class="token punctuation">)</span><span class="token punctuation">;</span>
            //准备Context
            Map<span class="token operator">&lt;</span>String,User<span class="token operator">></span> context <span class="token operator">=</span> new HashMap<span class="token operator">&lt;</span>String,User<span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            context.put<span class="token punctuation">(</span><span class="token string">"user1"</span>, new User<span class="token punctuation">(</span><span class="token string">"jack"</span>,18<span class="token punctuation">))</span><span class="token punctuation">;</span>
            context.put<span class="token punctuation">(</span><span class="token string">"user2"</span>, new User<span class="token punctuation">(</span><span class="token string">"rose"</span>,22<span class="token punctuation">))</span><span class="token punctuation">;</span>
        OgnlContext oc <span class="token operator">=</span> new OgnlContext<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        oc.setRoot<span class="token punctuation">(</span>rootUser<span class="token punctuation">)</span><span class="token punctuation">;</span>
        oc.setValues<span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">;</span>
        //书写OGNL

        //将root中的user对象的name属性赋值
        Ognl.getValue<span class="token punctuation">(</span><span class="token string">"name='jerry'"</span>, oc, oc.getRoot<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
        String name <span class="token operator">=</span> <span class="token punctuation">(</span>String<span class="token punctuation">)</span> Ognl.getValue<span class="token punctuation">(</span><span class="token string">"name"</span>, oc, oc.getRoot<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>

        String name2 <span class="token operator">=</span> <span class="token punctuation">(</span>String<span class="token punctuation">)</span> Ognl.getValue<span class="token punctuation">(</span>"#user1.name<span class="token operator">=</span><span class="token string">'郝强勇'</span>,<span class="token comment" spellcheck="true">#user1.name", oc, oc.getRoot());</span>
        System.out.println<span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        System.out.println<span class="token punctuation">(</span>name2<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    @Test
    //基本语法演示
    //调用方法
    public void fun5<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception<span class="token punctuation">{</span>
        //准备ONGLContext
            //准备Root
            User rootUser <span class="token operator">=</span> new User<span class="token punctuation">(</span><span class="token string">"tom"</span>,18<span class="token punctuation">)</span><span class="token punctuation">;</span>
            //准备Context
            Map<span class="token operator">&lt;</span>String,User<span class="token operator">></span> context <span class="token operator">=</span> new HashMap<span class="token operator">&lt;</span>String,User<span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            context.put<span class="token punctuation">(</span><span class="token string">"user1"</span>, new User<span class="token punctuation">(</span><span class="token string">"jack"</span>,18<span class="token punctuation">))</span><span class="token punctuation">;</span>
            context.put<span class="token punctuation">(</span><span class="token string">"user2"</span>, new User<span class="token punctuation">(</span><span class="token string">"rose"</span>,22<span class="token punctuation">))</span><span class="token punctuation">;</span>
        OgnlContext oc <span class="token operator">=</span> new OgnlContext<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        oc.setRoot<span class="token punctuation">(</span>rootUser<span class="token punctuation">)</span><span class="token punctuation">;</span>
        oc.setValues<span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">;</span>
        //书写OGNL

        //调用root中user对象的setName方法
        Ognl.getValue<span class="token punctuation">(</span><span class="token string">"setName('lilei')"</span>, oc, oc.getRoot<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
        String name <span class="token operator">=</span> <span class="token punctuation">(</span>String<span class="token punctuation">)</span> Ognl.getValue<span class="token punctuation">(</span><span class="token string">"getName()"</span>, oc, oc.getRoot<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>

        String name2 <span class="token operator">=</span> <span class="token punctuation">(</span>String<span class="token punctuation">)</span> Ognl.getValue<span class="token punctuation">(</span>"#user1.setName<span class="token punctuation">(</span><span class="token string">'lucy'</span><span class="token punctuation">)</span>,<span class="token comment" spellcheck="true">#user1.getName()", oc, oc.getRoot());</span>


        System.out.println<span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        System.out.println<span class="token punctuation">(</span>name2<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>


    @Test
    //基本语法演示
    //调用静态方法
    public void fun6<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception<span class="token punctuation">{</span>
        //准备ONGLContext
            //准备Root
            User rootUser <span class="token operator">=</span> new User<span class="token punctuation">(</span><span class="token string">"tom"</span>,18<span class="token punctuation">)</span><span class="token punctuation">;</span>
            //准备Context
            Map<span class="token operator">&lt;</span>String,User<span class="token operator">></span> context <span class="token operator">=</span> new HashMap<span class="token operator">&lt;</span>String,User<span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            context.put<span class="token punctuation">(</span><span class="token string">"user1"</span>, new User<span class="token punctuation">(</span><span class="token string">"jack"</span>,18<span class="token punctuation">))</span><span class="token punctuation">;</span>
            context.put<span class="token punctuation">(</span><span class="token string">"user2"</span>, new User<span class="token punctuation">(</span><span class="token string">"rose"</span>,22<span class="token punctuation">))</span><span class="token punctuation">;</span>
        OgnlContext oc <span class="token operator">=</span> new OgnlContext<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        oc.setRoot<span class="token punctuation">(</span>rootUser<span class="token punctuation">)</span><span class="token punctuation">;</span>
        oc.setValues<span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">;</span>
        //书写OGNL

        String name <span class="token operator">=</span> <span class="token punctuation">(</span>String<span class="token punctuation">)</span> Ognl.getValue<span class="token punctuation">(</span><span class="token string">"@cn.itheima.a_ognl.HahaUtils@echo('hello 强勇!')"</span>, oc, oc.getRoot<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
        //Double pi <span class="token operator">=</span> <span class="token punctuation">(</span>Double<span class="token punctuation">)</span> Ognl.getValue<span class="token punctuation">(</span><span class="token string">"@java.lang.Math@PI"</span>, oc, oc.getRoot<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
        Double pi <span class="token operator">=</span> <span class="token punctuation">(</span>Double<span class="token punctuation">)</span> Ognl.getValue<span class="token punctuation">(</span><span class="token string">"@@PI"</span>, oc, oc.getRoot<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
        System.out.println<span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        System.out.println<span class="token punctuation">(</span>pi<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>    

    @Test
    //基本语法演示
    //ognl创建对象-list<span class="token operator">|</span>map
    public void fun7<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception<span class="token punctuation">{</span>
        //准备ONGLContext
            //准备Root
            User rootUser <span class="token operator">=</span> new User<span class="token punctuation">(</span><span class="token string">"tom"</span>,18<span class="token punctuation">)</span><span class="token punctuation">;</span>
            //准备Context
            Map<span class="token operator">&lt;</span>String,User<span class="token operator">></span> context <span class="token operator">=</span> new HashMap<span class="token operator">&lt;</span>String,User<span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            context.put<span class="token punctuation">(</span><span class="token string">"user1"</span>, new User<span class="token punctuation">(</span><span class="token string">"jack"</span>,18<span class="token punctuation">))</span><span class="token punctuation">;</span>
            context.put<span class="token punctuation">(</span><span class="token string">"user2"</span>, new User<span class="token punctuation">(</span><span class="token string">"rose"</span>,22<span class="token punctuation">))</span><span class="token punctuation">;</span>
        OgnlContext oc <span class="token operator">=</span> new OgnlContext<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        oc.setRoot<span class="token punctuation">(</span>rootUser<span class="token punctuation">)</span><span class="token punctuation">;</span>
        oc.setValues<span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">;</span>
        //书写OGNL

        //创建list对象
        Integer size <span class="token operator">=</span> <span class="token punctuation">(</span>Integer<span class="token punctuation">)</span> Ognl.getValue<span class="token punctuation">(</span><span class="token string">"{'tom','jerry','jack','rose'}.size()"</span>, oc, oc.getRoot<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
        String name <span class="token operator">=</span> <span class="token punctuation">(</span>String<span class="token punctuation">)</span> Ognl.getValue<span class="token punctuation">(</span><span class="token string">"{'tom','jerry','jack','rose'}[0]"</span>, oc, oc.getRoot<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
        String name2 <span class="token operator">=</span> <span class="token punctuation">(</span>String<span class="token punctuation">)</span> Ognl.getValue<span class="token punctuation">(</span><span class="token string">"{'tom','jerry','jack','rose'}.get(1)"</span>, oc, oc.getRoot<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>

        /*System.out.println<span class="token punctuation">(</span>size<span class="token punctuation">)</span><span class="token punctuation">;</span>
        System.out.println<span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
        System.out.println<span class="token punctuation">(</span>name2<span class="token punctuation">)</span><span class="token punctuation">;</span>*/
        //创建Map对象
        Integer size2 <span class="token operator">=</span> <span class="token punctuation">(</span>Integer<span class="token punctuation">)</span> Ognl.getValue<span class="token punctuation">(</span><span class="token string">"#{'name':'tom','age':18}.size()"</span>, oc, oc.getRoot<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
        String name3  <span class="token operator">=</span> <span class="token punctuation">(</span>String<span class="token punctuation">)</span> Ognl.getValue<span class="token punctuation">(</span><span class="token string">"#{'name':'tom','age':18}['name']"</span>, oc, oc.getRoot<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
        Integer age  <span class="token operator">=</span> <span class="token punctuation">(</span>Integer<span class="token punctuation">)</span> Ognl.getValue<span class="token punctuation">(</span><span class="token string">"#{'name':'tom','age':18}.get('age')"</span>, oc, oc.getRoot<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
        System.out.println<span class="token punctuation">(</span>size2<span class="token punctuation">)</span><span class="token punctuation">;</span>
        System.out.println<span class="token punctuation">(</span>name3<span class="token punctuation">)</span><span class="token punctuation">;</span>
        System.out.println<span class="token punctuation">(</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>    

<span class="token punctuation">}</span>
</code></pre>
<h2 id="jsp"><a href="#jsp" class="headerlink" title="jsp"></a>jsp</h2><pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span><span class="token operator">!</span>-- 调试标签 --<span class="token operator">></span>
<span class="token operator">&lt;</span>s:debug<span class="token operator">></span><span class="token operator">&lt;</span>/s:debug<span class="token operator">></span>
</code></pre>
<p> 项目路劲</p>
<pre class=" language-bash"><code class="language-bash">     <span class="token operator">&lt;</span>form action<span class="token operator">=</span><span class="token string">"<span class="token variable">${pageContext.request.contextPath}</span>/Demo2Action"</span><span class="token operator">></span>
        用户名:<span class="token operator">&lt;</span>input type<span class="token operator">=</span><span class="token string">"text"</span> name<span class="token operator">=</span><span class="token string">"name"</span> /<span class="token operator">></span><span class="token operator">&lt;</span>br<span class="token operator">></span>
        <span class="token operator">&lt;</span>input type<span class="token operator">=</span><span class="token string">"submit"</span> value<span class="token operator">=</span><span class="token string">"提交"</span> /<span class="token operator">></span>
    <span class="token operator">&lt;</span>/form<span class="token operator">></span>
</code></pre>
<h2 id="Struts2标签"><a href="#Struts2标签" class="headerlink" title="Struts2标签"></a>Struts2标签</h2><h2 id="国际化"><a href="#国际化" class="headerlink" title="国际化"></a>国际化</h2><p>struts2 中使用的 properties 文件来做国际化<br>全局资源包</p>
<pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span><span class="token operator">!</span>--
在struts.xml 中配置常量指定全局字符串包位置
--<span class="token operator">></span>
<span class="token operator">&lt;</span>constant name<span class="token operator">=</span><span class="token string">"struts.custom.i18n.resources"</span> value<span class="token operator">=</span><span class="token string">"com.ittianyu.i18n.strings"</span> /<span class="token operator">></span>
</code></pre>
<ol>
<li>包范围的资源包：把 资源包放在某包下面，命名为：package_语言代码_国家代码.properties</li>
<li>局部消息资源包：把 资源包放在某动作类路径下，命名为：动作类名称<em>语言代码</em>国家代码.properties <br><br>资源包的使用顺序：局部 &gt; 包范围 &gt; 全局！</li>
</ol>
<h3 id="读取资源包"><a href="#读取资源包" class="headerlink" title="读取资源包"></a>读取资源包</h3><pre class=" language-bash"><code class="language-bash">Action 

public class I18nAction extends ActionSupport <span class="token punctuation">{</span> 
public String execute<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
String value <span class="token operator">=</span> getText<span class="token punctuation">(</span><span class="token string">"key"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span> 
<span class="token punctuation">}</span> 
jsp 

<span class="token operator">&lt;</span>s:text name<span class="token operator">=</span><span class="token string">"key"</span>/<span class="token operator">></span> 
</code></pre>
<h3 id="手动指定读取的资源包"><a href="#手动指定读取的资源包" class="headerlink" title="手动指定读取的资源包"></a>手动指定读取的资源包</h3><p>当注定的包下没有找到指定的值时，会按顺序搜索配置了的资源包</p>
<pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span>s:i18n name<span class="token operator">=</span><span class="token string">"com.xxxx.package"</span><span class="token operator">></span>
    <span class="token operator">&lt;</span>s:text name<span class="token operator">=</span><span class="token string">"key"</span>/<span class="token operator">></span>
<span class="token operator">&lt;</span>/s:i18n<span class="token operator">></span>
</code></pre>
<h2 id="文件上传下载"><a href="#文件上传下载" class="headerlink" title="文件上传下载"></a>文件上传下载</h2><pre class=" language-bash"><code class="language-bash"> html
<span class="token operator">&lt;</span>s:form action<span class="token operator">=</span><span class="token string">"upload.action"</span> enctype<span class="token operator">=</span><span class="token string">"multipart/form-data"</span><span class="token operator">></span>
    <span class="token operator">&lt;</span>s:textfield name<span class="token operator">=</span><span class="token string">"username"</span> label<span class="token operator">=</span><span class="token string">"用户名"</span>/<span class="token operator">></span>
    <span class="token operator">&lt;</span>s:file name<span class="token operator">=</span><span class="token string">"photo"</span> label<span class="token operator">=</span><span class="token string">"照片"</span>/<span class="token operator">></span>
    <span class="token operator">&lt;</span>s:submit value<span class="token operator">=</span><span class="token string">"上传"</span>/<span class="token operator">></span>
<span class="token operator">&lt;</span>/s:form<span class="token operator">></span>
</code></pre>
<pre class=" language-bash"><code class="language-bash"> action
public class UploadAction extends ActionSupport<span class="token punctuation">{</span>
    public String username<span class="token punctuation">;</span>
    public File photo<span class="token punctuation">;</span>
    public String photoFileName<span class="token punctuation">;</span>// 上传文件名。变量命名规格 字段名+FileName
    public String photoContentType<span class="token punctuation">;</span>// 上传文件的MIME类型。变量命名规格 字段名+ContentType

    public String upload<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        // 获取文件存储目录
        ServletContext servletContext <span class="token operator">=</span> ServletActionContext.getServletContext<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        String path <span class="token operator">=</span> servletContext.getRealPath<span class="token punctuation">(</span><span class="token string">"/WEB-INF/files"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        File <span class="token function">file</span> <span class="token operator">=</span> new File<span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>file.exists<span class="token punctuation">(</span><span class="token punctuation">))</span>
            file.mkdirs<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        // 存储到目标路径
        photo.renameTo<span class="token punctuation">(</span>new File<span class="token punctuation">(</span>file, photoFileName<span class="token punctuation">))</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> NONE<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<pre class=" language-bash"><code class="language-bash"> 修改上传文件大小限制
<span class="token operator">&lt;</span><span class="token operator">!</span>--限制上传最大尺寸为 1048576 byte--<span class="token operator">></span>
    <span class="token operator">&lt;</span>constant name<span class="token operator">=</span><span class="token string">"struts.multipart.maxSize"</span> value<span class="token operator">=</span><span class="token string">"1048576"</span>/<span class="token operator">></span>
</code></pre>
<pre class=" language-bash"><code class="language-bash">限制上传文件扩展名
<span class="token operator">&lt;</span>action name<span class="token operator">=</span><span class="token string">"upload"</span> class<span class="token operator">=</span><span class="token string">"com.ittianyu.javaeetest.web.action.UploadAction"</span> method<span class="token operator">=</span><span class="token string">"upload"</span><span class="token operator">></span>
    <span class="token operator">&lt;</span>interceptor-ref name<span class="token operator">=</span><span class="token string">"defaultStack"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"fileUpload.allowedExtensions"</span><span class="token operator">></span>jpg,png<span class="token operator">&lt;</span>/param<span class="token operator">></span>
    <span class="token operator">&lt;</span>/interceptor-ref<span class="token operator">></span>
    <span class="token operator">&lt;</span>result name<span class="token operator">=</span><span class="token string">"input"</span><span class="token operator">></span>upload.jsp<span class="token operator">&lt;</span>/result<span class="token operator">></span>
<span class="token operator">&lt;</span>/action<span class="token operator">></span>
</code></pre>
<pre class=" language-bash"><code class="language-bash">多文件上传需要把
public File photo<span class="token punctuation">;</span>
public String photoFileName<span class="token punctuation">;</span>// 上传文件名。变量命名规格 字段名+FileName
public String photoContentType<span class="token punctuation">;</span>// 上传文件的MIME类型。变量命名规格 字段名+ContentType

改成，也就是改成数组

public File<span class="token punctuation">[</span><span class="token punctuation">]</span> photo<span class="token punctuation">;</span>
public String<span class="token punctuation">[</span><span class="token punctuation">]</span> photoFileName<span class="token punctuation">;</span>// 上传文件名。变量命名规格 字段名+FileName
public String<span class="token punctuation">[</span><span class="token punctuation">]</span> photoContentType<span class="token punctuation">;</span>// 上传文件的MIME类型。变量命名规格 字段名+ContentType

</code></pre>
<pre class=" language-bash"><code class="language-bash">action
public class DownloadAction extends ActionSupport <span class="token punctuation">{</span>
    public InputStream inputStream<span class="token punctuation">;</span>
    public String filename<span class="token punctuation">;</span>

    public String download<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception<span class="token punctuation">{</span>
        // 找到文件路径
        String path <span class="token operator">=</span> ServletActionContext.getServletContext<span class="token punctuation">(</span><span class="token punctuation">)</span>.getRealPath<span class="token punctuation">(</span><span class="token string">"/WEB-INF/files/1.jpg"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        // 包装成流
        inputStream <span class="token operator">=</span> new FileInputStream<span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>
        // 设置浏览器接收时文件名
        filename <span class="token operator">=</span> <span class="token string">"图片.jpg"</span><span class="token punctuation">;</span>

        <span class="token keyword">return</span> SUCCESS<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<pre class=" language-bash"><code class="language-bash">配置
<span class="token operator">&lt;</span>action name<span class="token operator">=</span><span class="token string">"download"</span> class<span class="token operator">=</span><span class="token string">"com.ittianyu.javaeetest.web.action.DownloadAction"</span> method<span class="token operator">=</span><span class="token string">"download"</span><span class="token operator">></span>
    <span class="token operator">&lt;</span>result name<span class="token operator">=</span><span class="token string">"success"</span> type<span class="token operator">=</span><span class="token string">"stream"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">!</span>--下载类型为 bin--<span class="token operator">></span>
        <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"contentType"</span><span class="token operator">></span>application/octet-stream<span class="token operator">&lt;</span>/param<span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">!</span>--下载打开方式--<span class="token operator">></span>
        <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"contentDisposition"</span><span class="token operator">></span>attachment<span class="token punctuation">;</span>filename<span class="token operator">=</span>$<span class="token punctuation">{</span>@java.net.URLEncoder@encode<span class="token punctuation">(</span>filename, <span class="token string">"UTF-8"</span><span class="token punctuation">)</span><span class="token punctuation">}</span>
        <span class="token operator">&lt;</span>/param<span class="token operator">></span>
        <span class="token operator">&lt;</span><span class="token operator">!</span>--流名称--<span class="token operator">></span>
        <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"inputName"</span><span class="token operator">></span>inputStream<span class="token operator">&lt;</span>/param<span class="token operator">></span>
    <span class="token operator">&lt;</span>/result<span class="token operator">></span>
<span class="token operator">&lt;</span>/action<span class="token operator">></span>
</code></pre>
<h2 id="Validator"><a href="#Validator" class="headerlink" title="Validator"></a>Validator</h2><p>struts2为我们共内置了16个验证器，且全部是基于字段的验证器</p>
<h3 id="required"><a href="#required" class="headerlink" title="required"></a>required</h3><p>验证字段的值是不是 null。注意，不是空字符串或空白字符串</p>
<pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span>validators<span class="token operator">></span>
    <span class="token operator">&lt;</span>field name<span class="token operator">=</span><span class="token string">"password"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>field-validator type<span class="token operator">=</span><span class="token string">"required"</span><span class="token operator">></span>
            <span class="token operator">&lt;</span>message<span class="token operator">></span>The password field is required<span class="token operator">!</span><span class="token operator">&lt;</span>/message<span class="token operator">></span>
        <span class="token operator">&lt;</span>/field-validator<span class="token operator">></span>
    <span class="token operator">&lt;</span>/field<span class="token operator">></span>
<span class="token operator">&lt;</span>/validators<span class="token operator">></span>
</code></pre>
<h3 id="requiredstring"><a href="#requiredstring" class="headerlink" title="requiredstring"></a>requiredstring</h3><p>验证字段的值既不是null、也不是空白。<br>参数： </p>
<ul>
<li>fieldName：要验证的字段名 </li>
<li>trim：是否去掉首尾空格<pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span>validators<span class="token operator">></span>
  <span class="token operator">&lt;</span>field name<span class="token operator">=</span><span class="token string">"userName"</span><span class="token operator">></span>
      <span class="token operator">&lt;</span>field-validator type<span class="token operator">=</span><span class="token string">"requiredstring"</span><span class="token operator">></span>
          <span class="token operator">&lt;</span>message<span class="token operator">></span>Please input the userName<span class="token operator">!</span><span class="token operator">&lt;</span>/message<span class="token operator">></span>
      <span class="token operator">&lt;</span>/field-validator<span class="token operator">></span>
  <span class="token operator">&lt;</span>/field<span class="token operator">></span>
  <span class="token operator">&lt;</span>field name<span class="token operator">=</span><span class="token string">"password"</span><span class="token operator">></span>
      <span class="token operator">&lt;</span>field-validator type<span class="token operator">=</span><span class="token string">"requiredstring"</span><span class="token operator">></span>
          <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"trim"</span><span class="token operator">></span>false<span class="token operator">&lt;</span>/param<span class="token operator">></span>
          <span class="token operator">&lt;</span>message<span class="token operator">></span>Please input the password<span class="token operator">!</span><span class="token operator">&lt;</span>/message<span class="token operator">></span>
      <span class="token operator">&lt;</span>/field-validator<span class="token operator">></span>
  <span class="token operator">&lt;</span>/field<span class="token operator">></span>
<span class="token operator">&lt;</span>/validators<span class="token operator">></span>
</code></pre>
</li>
</ul>
<h3 id="int-amp-long-short"><a href="#int-amp-long-short" class="headerlink" title="int&amp;long short"></a>int&amp;long short</h3><p>验证某个字段的值是否可以被转换为一个整数。还可以验证是否在允许的范围内。</p>
<p>参数： </p>
<ul>
<li>fieldName：要验证的字段名 </li>
<li>min：允许的最小值 </li>
<li>max：允许的最大值<br>基于字段的验证<pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span>validators<span class="token operator">></span>
  <span class="token operator">&lt;</span>field name<span class="token operator">=</span><span class="token string">"age"</span><span class="token operator">></span>
      <span class="token operator">&lt;</span>field-validator type<span class="token operator">=</span><span class="token string">"int"</span><span class="token operator">></span>
          <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"min"</span><span class="token operator">></span>18<span class="token operator">&lt;</span>/param<span class="token operator">></span>
          <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"max"</span><span class="token operator">></span>60<span class="token operator">&lt;</span>/param<span class="token operator">></span>
          <span class="token operator">&lt;</span>message<span class="token operator">></span>The age must be between <span class="token variable">${min}</span> and <span class="token variable">${max}</span><span class="token operator">&lt;</span>/message<span class="token operator">></span>
      <span class="token operator">&lt;</span>/field-validator<span class="token operator">></span>
  <span class="token operator">&lt;</span>/field<span class="token operator">></span>
<span class="token operator">&lt;</span>/validators<span class="token operator">></span>
</code></pre>
基于验证器的验证<pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span>validators<span class="token operator">></span>
  <span class="token operator">&lt;</span>validator type<span class="token operator">=</span><span class="token string">"int"</span><span class="token operator">></span>
      <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"fieldName"</span><span class="token operator">></span>age<span class="token operator">&lt;</span>/param<span class="token operator">></span>
      <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"min"</span><span class="token operator">></span>18<span class="token operator">&lt;</span>/param<span class="token operator">></span>
      <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"max"</span><span class="token operator">></span>60<span class="token operator">&lt;</span>/param<span class="token operator">></span>
      <span class="token operator">&lt;</span>message<span class="token operator">></span>The age must be between <span class="token variable">${min}</span> and <span class="token variable">${max}</span><span class="token operator">&lt;</span>/message<span class="token operator">></span>
  <span class="token operator">&lt;</span>/validator<span class="token operator">></span>
<span class="token operator">&lt;</span>/validators<span class="token operator">></span>
</code></pre>
</li>
</ul>
<h3 id="double"><a href="#double" class="headerlink" title="double"></a>double</h3><p>用来验证某个字段的值是否可以被转换为一个双精度浮点数。还可验证是否在允许的范围内。</p>
<p>参数： </p>
<ul>
<li>fieldName：要验证的字段名 </li>
<li>minInclusive：允许的最小值，包含最小值 </li>
<li>maxInclusive：允许的最大值，包含最大值 </li>
<li>minExclusive：允许的最小值，不包含最小值 </li>
<li>maxExclusive：允许的最大值，不包含最大值</li>
</ul>
<pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span>validators<span class="token operator">></span>
    <span class="token operator">&lt;</span>field name<span class="token operator">=</span><span class="token string">"percentage1"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>field-validator type<span class="token operator">=</span><span class="token string">"double"</span><span class="token operator">></span>
            <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"minInclusive"</span><span class="token operator">></span>20.1<span class="token operator">&lt;</span>/param<span class="token operator">></span>
            <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"maxInclusive"</span><span class="token operator">></span>50.1<span class="token operator">&lt;</span>/param<span class="token operator">></span>
            <span class="token operator">&lt;</span>message<span class="token operator">></span> The age must be between <span class="token variable">${ minInclusive }</span> and <span class="token variable">${ maxInclusive }</span><span class="token punctuation">(</span>含<span class="token punctuation">)</span><span class="token operator">&lt;</span>/message<span class="token operator">></span>
        <span class="token operator">&lt;</span>/field-validator<span class="token operator">></span>
    <span class="token operator">&lt;</span>/field<span class="token operator">></span>
    <span class="token operator">&lt;</span>field name<span class="token operator">=</span><span class="token string">"percentage2"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>field-validator type<span class="token operator">=</span><span class="token string">"double"</span><span class="token operator">></span>
            <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"minExclusive"</span><span class="token operator">></span>0.345<span class="token operator">&lt;</span>/param<span class="token operator">></span>
            <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"maxExclusive"</span><span class="token operator">></span>99.987<span class="token operator">&lt;</span>/param<span class="token operator">></span>
            <span class="token operator">&lt;</span>message<span class="token operator">></span> The age must be between <span class="token variable">${ minExclusive }</span> and <span class="token variable">${ maxExclusive }</span><span class="token punctuation">(</span>不含<span class="token punctuation">)</span><span class="token operator">&lt;</span>/message<span class="token operator">></span>
        <span class="token operator">&lt;</span>/field-validator<span class="token operator">></span>
    <span class="token operator">&lt;</span>/field<span class="token operator">></span>
<span class="token operator">&lt;</span>/validators<span class="token operator">></span>
</code></pre>
<h3 id="date"><a href="#date" class="headerlink" title="date"></a>date</h3><p>用来确保给定的日期字段的值在指定的范围内。</p>
<p>参数： </p>
<ul>
<li>fieldName：要验证的字段名 </li>
<li>min：允许的最小值，包含最小值 </li>
<li>max：允许的最大值，包含最大值</li>
</ul>
<pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span>validators<span class="token operator">></span>
    <span class="token operator">&lt;</span>field name<span class="token operator">=</span><span class="token string">"birthday"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>field-validator type<span class="token operator">=</span><span class="token string">"date"</span><span class="token operator">></span>
            <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"min"</span><span class="token operator">></span>2011-01-01<span class="token operator">&lt;</span>/param<span class="token operator">></span>
            <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"max"</span><span class="token operator">></span>2011-12-31<span class="token operator">&lt;</span>/param<span class="token operator">></span>
            <span class="token operator">&lt;</span>message<span class="token operator">></span>日期必须为2011年<span class="token operator">&lt;</span>/message<span class="token operator">></span>
        <span class="token operator">&lt;</span>/field-validator<span class="token operator">></span>
    <span class="token operator">&lt;</span>/field<span class="token operator">></span>
<span class="token operator">&lt;</span>/validators<span class="token operator">></span>
</code></pre>
<h3 id="expression"><a href="#expression" class="headerlink" title="expression"></a>expression</h3><p>用于验证是否满足一个OGNL表达式。这是一个非字段的验证。只有给定的参数的返回值是true时才能验证通过。验证不通过时产生一个动作错误，因此要显示该错误，需要使用标签。</p>
<pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span>validators<span class="token operator">></span>
    <span class="token operator">&lt;</span>validator type<span class="token operator">=</span><span class="token string">"expression"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"expression"</span><span class="token operator">></span>
            maxNumber<span class="token operator">></span>minNumber
        <span class="token operator">&lt;</span>/param<span class="token operator">></span>
        <span class="token operator">&lt;</span>message<span class="token operator">></span>最大值必须大于最小值<span class="token operator">&lt;</span>/message<span class="token operator">></span>
    <span class="token operator">&lt;</span>/validator<span class="token operator">></span>
<span class="token operator">&lt;</span>/validators<span class="token operator">></span>
</code></pre>
<h3 id="field-expression"><a href="#field-expression" class="headerlink" title="field expression"></a>field expression</h3><p>用于验证某个字段是否满足一个OGNL表达式。这是一个基于字段的验证。只有给定的参数的返回值是true时才能验证通过。验证不通过时产生一个字段错误。<br>参数： </p>
<ul>
<li>fieldName：要验证的字段名 </li>
<li>expression：OGNL表达式，只有该表达式为true才能验证通过</li>
</ul>
<pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span>validators<span class="token operator">></span>
    <span class="token operator">&lt;</span>field name<span class="token operator">=</span><span class="token string">"maxNumber"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>field-validator type<span class="token operator">=</span><span class="token string">"fieldexpression"</span><span class="token operator">></span>
            <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"expression"</span><span class="token operator">></span>
            maxNumber<span class="token operator">></span>100
        <span class="token operator">&lt;</span>/param<span class="token operator">></span>
        <span class="token operator">&lt;</span>message<span class="token operator">></span>最大值必须大于最小值1<span class="token operator">&lt;</span>/message<span class="token operator">></span>
        <span class="token operator">&lt;</span>/field-validator<span class="token operator">></span>
    <span class="token operator">&lt;</span>/field<span class="token operator">></span>
<span class="token operator">&lt;</span>/validators<span class="token operator">></span>
</code></pre>
<h3 id="email"><a href="#email" class="headerlink" title="email"></a>email</h3><p>用来验证给定的字段是否符合一个Email的规范。它的正则表达式为<br>\b(^<a href="\\.[_A-Za-z0-9-]">_A-Za-z0-9-</a>*@([A-Za-z0-9-])+((\.com)|(\.net)|(\.org)|(\.info)|(\.edu)|(\.mil)|(\.gov)|(\.biz)|(\.ws)|(\.us)|(\.tv)|(\.cc)|(\.aero)|(\.arpa)|(\.coop)|(\.int)|(\.jobs)|(\.museum)|(\.name)|(\.pro)|(\.travel)|(\.nato)|(\..{2,3})|(\..{2,3}\..{2,3}))$)\b</p>
<pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span>validators<span class="token operator">></span>
    <span class="token operator">&lt;</span>field name<span class="token operator">=</span><span class="token string">"email"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>field-validator type<span class="token operator">=</span><span class="token string">"email"</span><span class="token operator">></span>
            <span class="token operator">&lt;</span>message<span class="token operator">></span>请输入正确的邮箱<span class="token operator">&lt;</span>/message<span class="token operator">></span>
        <span class="token operator">&lt;</span>/field-validator<span class="token operator">></span>
    <span class="token operator">&lt;</span>/field<span class="token operator">></span>
<span class="token operator">&lt;</span>/validators<span class="token operator">></span>
</code></pre>
<h3 id="url"><a href="#url" class="headerlink" title="url"></a>url</h3><p>来验证给定的字段值是否是一个合法的URL地址</p>
<pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span>validators<span class="token operator">></span>
    <span class="token operator">&lt;</span>field name<span class="token operator">=</span><span class="token string">"url"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>field-validator type<span class="token operator">=</span><span class="token string">"url"</span><span class="token operator">></span>
            <span class="token operator">&lt;</span>message<span class="token operator">></span>请输入正确的地址<span class="token operator">&lt;</span>/message<span class="token operator">></span>
        <span class="token operator">&lt;</span>/field-validator<span class="token operator">></span>
    <span class="token operator">&lt;</span>/field<span class="token operator">></span>
<span class="token operator">&lt;</span>/validators<span class="token operator">></span>
</code></pre>
<h3 id="visitor"><a href="#visitor" class="headerlink" title="visitor"></a>visitor</h3><p>该验证程序可以提高代码的可重用性，你可以利用它把同一个验证程序配置文件用于多个动作</p>
<pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span>validators<span class="token operator">></span>
    <span class="token operator">&lt;</span>field name<span class="token operator">=</span><span class="token string">"streetName"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>field-validator type<span class="token operator">=</span><span class="token string">"requiredstring"</span><span class="token operator">></span>
            <span class="token operator">&lt;</span>message<span class="token operator">></span>请输入正确街道地址<span class="token operator">&lt;</span>/message<span class="token operator">></span>
        <span class="token operator">&lt;</span>/field-validator<span class="token operator">></span>
    <span class="token operator">&lt;</span>/field<span class="token operator">></span>
<span class="token operator">&lt;</span>/validators<span class="token operator">></span>

<span class="token operator">&lt;</span>validators<span class="token operator">></span>
    <span class="token operator">&lt;</span>field name<span class="token operator">=</span><span class="token string">"address"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>field-validator type<span class="token operator">=</span><span class="token string">"visitor"</span><span class="token operator">></span>
            <span class="token operator">&lt;</span>message<span class="token operator">></span>Address:<span class="token operator">&lt;</span>/message<span class="token operator">></span>
        <span class="token operator">&lt;</span>/field-validator<span class="token operator">></span>
    <span class="token operator">&lt;</span>/field<span class="token operator">></span>
<span class="token operator">&lt;</span>/validators<span class="token operator">></span>
</code></pre>
<h3 id="stringlength"><a href="#stringlength" class="headerlink" title="stringlength"></a>stringlength</h3><p>用来验证一个非空的字段值是不是有足够的长度。</p>
<h3 id="regex"><a href="#regex" class="headerlink" title="regex"></a>regex</h3><p>用来检查给定字段是否与给定的正则表达式相匹配。正则表达式的详细内容可以参考 JDK 的 java.util.regex.Pattern 类。</p>
<p>参数： </p>
<ul>
<li>fieldname：要验证的字段名 </li>
<li>expression：正则表达式 </li>
<li>caseSensitive：是否区分大小写的情况，默认 true </li>
<li>trim：是否去掉首尾空格，默认 true<pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span>validators<span class="token operator">></span>
  <span class="token operator">&lt;</span>field name<span class="token operator">=</span><span class="token string">"userName"</span><span class="token operator">></span>
      <span class="token operator">&lt;</span>field-validator type<span class="token operator">=</span><span class="token string">"regex"</span><span class="token operator">></span>
          <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"expression"</span><span class="token operator">></span><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token punctuation">[</span>CDATA<span class="token punctuation">[</span><span class="token punctuation">(</span><span class="token punctuation">[</span>aAbBcCdD<span class="token punctuation">]</span><span class="token punctuation">[</span>123<span class="token punctuation">]</span><span class="token punctuation">[</span>eEfFgG<span class="token punctuation">]</span><span class="token punctuation">[</span>456<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">></span><span class="token operator">&lt;</span>/param<span class="token operator">></span>
          <span class="token operator">&lt;</span>message<span class="token operator">></span> 用户名必须符合规范<span class="token operator">&lt;</span>/message<span class="token operator">></span>
      <span class="token operator">&lt;</span>/field-validator<span class="token operator">></span>
  <span class="token operator">&lt;</span>/field<span class="token operator">></span>
<span class="token operator">&lt;</span>/validators<span class="token operator">></span>
</code></pre>
<h2 id="拦截器Interceptor"><a href="#拦截器Interceptor" class="headerlink" title="拦截器Interceptor"></a>拦截器Interceptor</h2>Struts2 拦截器在访问某个 Action 方法之前或之后实施拦截, Struts2 拦截器是可插拔的, 拦截器是 AOP 的一种实现．</li>
</ul>
<h3 id="常用拦截器"><a href="#常用拦截器" class="headerlink" title="常用拦截器"></a>常用拦截器</h3><ol>
<li>conversionError：将错误从ActionContext中添加到Action的属性字段中</li>
<li>fileUpload：提供文件上传功能</li>
<li>i18n：记录用户选择的locale</li>
<li>model-driven：如果一个类实现了ModelDriven，将getModel得到的结果放在Value Stack中</li>
<li>params：将请求中的参数设置到Action中去</li>
<li>servletConfig：提供访问HttpServletRequest和HttpServletResponse的方法，以Map的方式访问</li>
<li>token：避免重复提交</li>
<li>validation：使用 action-validation.xml文件中定义的内容校验提交的数据</li>
<li>workflow：调用 Action 的 validate 方法，一旦有错误返回，重新定位到 INPUT 视图</li>
</ol>
<h3 id="自定义拦截器"><a href="#自定义拦截器" class="headerlink" title="自定义拦截器"></a>自定义拦截器</h3><pre class=" language-bash"><code class="language-bash">public class PermissionInterceptor implements Interceptor <span class="token punctuation">{</span> 
private static final long serialVersionUID <span class="token operator">=</span> -5178310397732210602L<span class="token punctuation">;</span> 
public void destroy<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
<span class="token punctuation">}</span> 
public void init<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
<span class="token punctuation">}</span> 
public String intercept<span class="token punctuation">(</span>ActionInvocation invocation<span class="token punctuation">)</span> throws Exception <span class="token punctuation">{</span> 
System.out.println<span class="token punctuation">(</span><span class="token string">"进入拦截器"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
if<span class="token punctuation">(</span>session里存在用户<span class="token punctuation">)</span><span class="token punctuation">{</span> 
String result <span class="token operator">=</span> invocation.invoke<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span>else<span class="token punctuation">{</span> 
<span class="token keyword">return</span> “logon”<span class="token punctuation">;</span> 
<span class="token punctuation">}</span> 
//System.out.println<span class="token punctuation">(</span><span class="token string">"返回值:"</span>+ result<span class="token punctuation">)</span><span class="token punctuation">;</span> 
//return result<span class="token punctuation">;</span> 
<span class="token punctuation">}</span> 
<span class="token punctuation">}</span> 
</code></pre>
<p>在 struts.xml 文件中配置自定义的拦截器 </p>
<pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span>package name<span class="token operator">=</span><span class="token string">"itcast"</span> namespace<span class="token operator">=</span><span class="token string">"/test"</span> extends<span class="token operator">=</span><span class="token string">"struts-default"</span><span class="token operator">></span> 
<span class="token operator">&lt;</span>interceptors<span class="token operator">></span> 
<span class="token operator">&lt;</span>interceptor name<span class="token operator">=</span>“permission<span class="token string">" class="</span>cn.itcast.aop.PermissionInterceptor<span class="token string">" /> 
&lt;interceptor-stack name="</span>permissionStack<span class="token string">"> 
&lt;interceptor-ref name="</span>defaultStack<span class="token string">" /> 
&lt;interceptor-ref name="</span> permission <span class="token string">" /> 
&lt;/interceptor-stack> 
&lt;/interceptors> 
&lt;action name="</span>helloworld_*<span class="token string">" class="</span>cn.itcast.action.HelloWorldAction<span class="token string">" method="</span><span class="token punctuation">{</span>1<span class="token punctuation">}</span><span class="token string">"> 
&lt;result name="</span>success<span class="token string">">/WEB-INF/page/hello.jsp&lt;/result> 
&lt;interceptor-ref name="</span>permissionStack"/<span class="token operator">></span> 
<span class="token operator">&lt;</span>/action<span class="token operator">></span> 
<span class="token operator">&lt;</span>/package<span class="token operator">></span> 
</code></pre>
<h2 id="Action响应ajax请求"><a href="#Action响应ajax请求" class="headerlink" title="Action响应ajax请求"></a>Action响应ajax请求</h2><h3 id="发送ajax请求使用stream进行响应"><a href="#发送ajax请求使用stream进行响应" class="headerlink" title="发送ajax请求使用stream进行响应"></a>发送ajax请求使用stream进行响应</h3><pre class=" language-bash"><code class="language-bash">定义Action
public class UserAction
 <span class="token punctuation">{</span>   
     private String <span class="token function">uname</span><span class="token punctuation">;</span>   
     //声明输入流对象
     private InputStream inputStream<span class="token punctuation">;</span>
     public InputStream getInputStream<span class="token punctuation">(</span><span class="token punctuation">)</span>
     <span class="token punctuation">{</span>
         System.out.println<span class="token punctuation">(</span><span class="token string">"-------------getInputStream"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">return</span> inputStream<span class="token punctuation">;</span>
     <span class="token punctuation">}</span> 
     public String getUname<span class="token punctuation">(</span><span class="token punctuation">)</span>
     <span class="token punctuation">{</span>
         <span class="token keyword">return</span> <span class="token function">uname</span><span class="token punctuation">;</span>
     <span class="token punctuation">}</span> 
     public void setUname<span class="token punctuation">(</span>String uname<span class="token punctuation">)</span>
     <span class="token punctuation">{</span>
         this.uname <span class="token operator">=</span> <span class="token function">uname</span><span class="token punctuation">;</span>
     <span class="token punctuation">}</span> 
     //org.apache.struts2.dispatcher.StreamResult
     /**验证用户名的唯一性***/
     public String unameIsExists<span class="token punctuation">(</span><span class="token punctuation">)</span>
     <span class="token punctuation">{</span>
         System.out.println<span class="token punctuation">(</span><span class="token string">"----------unameIsExists"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         List<span class="token operator">&lt;</span>String<span class="token operator">></span> list <span class="token operator">=</span> new ArrayList<span class="token operator">&lt;</span>String<span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>       
         list.add<span class="token punctuation">(</span><span class="token string">"admin"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         list.add<span class="token punctuation">(</span><span class="token string">"lisi"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         String msg<span class="token operator">=</span>null<span class="token punctuation">;</span>
        if<span class="token punctuation">(</span>list.contains<span class="token punctuation">(</span>uname<span class="token punctuation">))</span>
         <span class="token punctuation">{</span>
             msg<span class="token operator">=</span><span class="token string">"用户名可用..."</span><span class="token punctuation">;</span>
         <span class="token punctuation">}</span>
         <span class="token keyword">else</span>
         <span class="token punctuation">{</span>
             msg<span class="token operator">=</span><span class="token string">"用户名可用..."</span><span class="token punctuation">;</span>
         <span class="token punctuation">}</span>
         //将msg响应到客户端,将msg中的数据封装到InputStream
         try
         <span class="token punctuation">{</span>
             inputStream<span class="token operator">=</span>new ByteArrayInputStream<span class="token punctuation">(</span>msg.getBytes<span class="token punctuation">(</span><span class="token string">"UTF-8"</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
         <span class="token punctuation">}</span>
         catch <span class="token punctuation">(</span>Exception e<span class="token punctuation">)</span>
         <span class="token punctuation">{</span>
             // TODO Auto-generated catch block
             e.printStackTrace<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token punctuation">}</span>
         <span class="token keyword">return</span> <span class="token string">"ajax"</span><span class="token punctuation">;</span>
     <span class="token punctuation">}</span>
 <span class="token punctuation">}</span>
</code></pre>
<pre class=" language-bash"><code class="language-bash">配置UserAction
 <span class="token operator">&lt;</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"UTF-8"</span>?<span class="token operator">></span>
   <span class="token operator">&lt;</span><span class="token operator">!</span>DOCTYPE struts PUBLIC
      <span class="token string">"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"</span>
      <span class="token string">"http://struts.apache.org/dtds/struts-2.3.dtd"</span><span class="token operator">></span>
 <span class="token operator">&lt;</span>struts<span class="token operator">></span>
     <span class="token operator">&lt;</span>package name<span class="token operator">=</span><span class="token string">"user"</span> namespace<span class="token operator">=</span><span class="token string">"/"</span> extends<span class="token operator">=</span><span class="token string">"struts-default"</span><span class="token operator">></span>
         <span class="token operator">&lt;</span>action name<span class="token operator">=</span><span class="token string">"userAction_*"</span> class<span class="token operator">=</span><span class="token string">"com.guangsoft.action.UserAction"</span>
             method<span class="token operator">=</span><span class="token string">"{1}"</span><span class="token operator">></span>
             <span class="token operator">&lt;</span>result name<span class="token operator">=</span><span class="token string">"ajax"</span> type<span class="token operator">=</span><span class="token string">"stream"</span><span class="token operator">></span>
                 <span class="token operator">&lt;</span>param name<span class="token operator">=</span><span class="token string">"inputName"</span><span class="token operator">></span>inputStream<span class="token operator">&lt;</span>/param<span class="token operator">></span>
             <span class="token operator">&lt;</span>/result<span class="token operator">></span>
         <span class="token operator">&lt;</span>/action<span class="token operator">></span>
     <span class="token operator">&lt;</span>/package<span class="token operator">></span>
 <span class="token operator">&lt;</span>/struts<span class="token operator">></span>
</code></pre>
<pre class=" language-bash"><code class="language-bash">实现UI页面
<span class="token operator">&lt;</span>script type<span class="token operator">=</span><span class="token string">"text/javascript"</span> src<span class="token operator">=</span><span class="token string">"js/jquery-1.8.3.min.js"</span><span class="token operator">></span><span class="token operator">&lt;</span>/script<span class="token operator">></span>
 <span class="token operator">&lt;</span>script type<span class="token operator">=</span><span class="token string">"text/javascript"</span><span class="token operator">></span>
     <span class="token keyword">function</span> sendReuqest<span class="token punctuation">(</span><span class="token punctuation">)</span>
     <span class="token punctuation">{</span>
     var uname<span class="token operator">=</span><span class="token punctuation">$(</span><span class="token string">"#uname"</span><span class="token punctuation">)</span>.val<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     $.post<span class="token punctuation">(</span><span class="token string">"userAction_unameIsExists.action"</span>,<span class="token string">"uname="</span>+uname,function<span class="token punctuation">(</span>data<span class="token punctuation">)</span>
     <span class="token punctuation">{</span>
     <span class="token punctuation">$(</span><span class="token string">"#sp"</span><span class="token punctuation">)</span>.html<span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token punctuation">}</span>
 <span class="token operator">&lt;</span>/script<span class="token operator">></span>
<span class="token operator">&lt;</span>/head<span class="token operator">></span>
 <span class="token operator">&lt;</span>body<span class="token operator">></span>
     <span class="token operator">&lt;</span>input type<span class="token operator">=</span><span class="token string">"text"</span> id<span class="token operator">=</span><span class="token string">"uname"</span> onblur<span class="token operator">=</span><span class="token string">"sendReuqest()"</span> /<span class="token operator">></span>
     <span class="token operator">&lt;</span>span id<span class="token operator">=</span><span class="token string">"sp"</span><span class="token operator">></span><span class="token operator">&lt;</span>/span<span class="token operator">></span>
 <span class="token operator">&lt;</span>/body<span class="token operator">></span>
 当请求发送到服务器上，先执行处理请求的方法，通过处理请求的方法的返回值，查找对应的result,如果result的type属性为stream,自动调用inputName属性对应的值对应的get方法，获得流对象。
</code></pre>
<h3 id="发送ajax请求使用json响应"><a href="#发送ajax请求使用json响应" class="headerlink" title="发送ajax请求使用json响应"></a>发送ajax请求使用json响应</h3><pre class=" language-bash"><code class="language-bash">加入jar包
struts2-json-plugin-2.3.16.1.jar
建立Action
 public class UserAction2
 <span class="token punctuation">{</span> 
     UsersDao dao <span class="token operator">=</span> new UsersDaoImpl<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     private Users user<span class="token punctuation">;</span>
     private List<span class="token operator">&lt;</span>Users<span class="token operator">></span> ulist<span class="token punctuation">;</span>
     //将ulist集合作为json对象的集合响应到客户端
     public List<span class="token operator">&lt;</span>Users<span class="token operator">></span> getUlist<span class="token punctuation">(</span><span class="token punctuation">)</span>
     <span class="token punctuation">{</span>
         System.out.println<span class="token punctuation">(</span><span class="token string">"-----------getUlist"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token keyword">return</span> ulist<span class="token punctuation">;</span>
     <span class="token punctuation">}</span>
     public Users getUser<span class="token punctuation">(</span><span class="token punctuation">)</span>
     <span class="token punctuation">{</span>
         <span class="token keyword">return</span> user<span class="token punctuation">;</span>
     <span class="token punctuation">}</span>
     public void setUser<span class="token punctuation">(</span>Users user<span class="token punctuation">)</span>
     <span class="token punctuation">{</span>
         this.user <span class="token operator">=</span> user<span class="token punctuation">;</span>
     <span class="token punctuation">}</span>   
     //org.apache.struts2.json.JSONInterceptor
     /***验证用户名是否可以：将不可用的用户名全部响应到客户端**/
     public String unameExistsList<span class="token punctuation">(</span><span class="token punctuation">)</span>
     <span class="token punctuation">{</span>
         System.out.println<span class="token punctuation">(</span><span class="token string">"-------------unameExistsList"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         ulist<span class="token operator">=</span>dao.selectUanemByUname<span class="token punctuation">(</span>user.getUname<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
         //将ulist集合作为json对象的集合响应到客户端
         <span class="token keyword">return</span> <span class="token string">"ajax"</span><span class="token punctuation">;</span>       
     <span class="token punctuation">}</span>

</code></pre>
<pre class=" language-bash"><code class="language-bash">建立UI页面 
 <span class="token operator">&lt;</span>head<span class="token operator">></span>
     <span class="token operator">&lt;</span>script<span class="token operator">></span>
         <span class="token keyword">function</span> sendReuqest<span class="token punctuation">(</span><span class="token punctuation">)</span>
         <span class="token punctuation">{</span>
             var uname<span class="token operator">=</span><span class="token punctuation">$(</span><span class="token string">"#uname"</span><span class="token punctuation">)</span>.val<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
             $.post<span class="token punctuation">(</span><span class="token string">"userAction2_unameExistsList.action"</span>,<span class="token string">"user.uname="</span>+uname,function<span class="token punctuation">(</span>data<span class="token punctuation">)</span>
             <span class="token punctuation">{</span>
                 //alert<span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span>
                 var div<span class="token operator">=</span><span class="token punctuation">$(</span><span class="token string">"#div"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                 div.html<span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span> //清空
                 //对json集合进行遍历
                 <span class="token variable"><span class="token variable">$(</span>data<span class="token variable">)</span></span>.each<span class="token punctuation">(</span>function<span class="token punctuation">(</span>index,item<span class="token punctuation">)</span>
                 <span class="token punctuation">{</span>
                     //alert<span class="token punctuation">(</span>index+<span class="token string">" "</span>+item<span class="token punctuation">)</span><span class="token punctuation">;</span>
                     //div.html<span class="token punctuation">(</span>item.uname<span class="token punctuation">)</span><span class="token punctuation">;</span>
                     div.append<span class="token punctuation">(</span><span class="token string">"
                     &lt;div>"</span>+item.uname+<span class="token string">"&lt;/div>
                     "</span><span class="token punctuation">)</span>
                 <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
             <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token punctuation">}</span>
     <span class="token operator">&lt;</span>/script<span class="token operator">></span>
 <span class="token operator">&lt;</span>/head<span class="token operator">></span>
 <span class="token operator">&lt;</span>body<span class="token operator">></span>
     <span class="token operator">&lt;</span><span class="token operator">!</span>-- <span class="token operator">&lt;</span>input type<span class="token operator">=</span><span class="token string">"text"</span> id<span class="token operator">=</span><span class="token string">"uname"</span> onblur<span class="token operator">=</span><span class="token string">"sendReuqest()"</span>/<span class="token operator">></span><span class="token operator">&lt;</span>span id<span class="token operator">=</span><span class="token string">"sp"</span><span class="token operator">></span><span class="token operator">&lt;</span>/span<span class="token operator">></span> --<span class="token operator">></span>
     <span class="token operator">&lt;</span>input type<span class="token operator">=</span><span class="token string">"text"</span> id<span class="token operator">=</span><span class="token string">"uname"</span> onkeyup<span class="token operator">=</span><span class="token string">"sendReuqest()"</span> /<span class="token operator">></span>
     <span class="token operator">&lt;</span>div style<span class="token operator">=</span><span class="token string">"border:  solid 1px red; width: 20%;margin-top: 5px"</span> id<span class="token operator">=</span><span class="token string">"div"</span><span class="token operator">></span>
     <span class="token operator">&lt;</span>/div<span class="token operator">></span>
 <span class="token operator">&lt;</span>/body<span class="token operator">></span>
</code></pre>
<p>将需要响应到客户端的数据封装为inputStream对象</p>
<p> 将msg的内容封装为InputStream对象</p>
<p> 结果的类型必须为stream，将流对象的名字赋值给inputName属性</p>
<p> 将需要使用json格式响应到客户端的数据封装到list集合</p>
<p> 给ulist属性赋值，自动调用ulist对应的get方法</p>
<h2 id="Struts2的零配置-注解"><a href="#Struts2的零配置-注解" class="headerlink" title="Struts2的零配置(注解)"></a>Struts2的零配置(注解)</h2><h3 id="基本使用"><a href="#基本使用" class="headerlink" title="基本使用"></a>基本使用</h3><p>1.struts2通过一个插件：struts2-convention-plugin-2.3.20.jar</p>
<ol start="2">
<li>该插件可以完全抛弃struts2的xml配置文件，甚至连注解也不用写<br>在 Action 类上加上 @ParentPackage(“struts-default”)<br>在对应的方法上加上<pre class=" language-bash"><code class="language-bash">@Action<span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"hello"</span>, results <span class="token operator">=</span> <span class="token punctuation">{</span>
        @Result<span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">"success"</span>, location<span class="token operator">=</span><span class="token string">"/hello.jsp"</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>
<pre class=" language-bash"><code class="language-bash">@ParentPackage<span class="token punctuation">(</span><span class="token string">"struts-default"</span><span class="token punctuation">)</span>
public class HelloAction extends ActionSupport <span class="token punctuation">{</span>
 @Action<span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"hello"</span>, results <span class="token operator">=</span> <span class="token punctuation">{</span>
            @Result<span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">"success"</span>, location<span class="token operator">=</span><span class="token string">"/hello.jsp"</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
 public String hello<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception <span class="token punctuation">{</span>
     System.out.println<span class="token punctuation">(</span><span class="token string">"hello world!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
     <span class="token keyword">return</span> SUCCESS<span class="token punctuation">;</span>
 <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
</li>
</ol>
<h3 id="向-Action-传递参数"><a href="#向-Action-传递参数" class="headerlink" title="向 Action 传递参数"></a>向 Action 传递参数</h3><p>登录是很常见的 action，这个时候一般要向服务器传递 username, password 等。</p>
<pre class=" language-bash"><code class="language-bash">public class User <span class="token punctuation">{</span>
    private String username<span class="token punctuation">;</span>
    private String password<span class="token punctuation">;</span>
    public String getUsername<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> username<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    public void setUsername<span class="token punctuation">(</span>String username<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        this.username <span class="token operator">=</span> username<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    public String getPassword<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> password<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    public void setPassword<span class="token punctuation">(</span>String password<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        this.password <span class="token operator">=</span> password<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>创建一个 UserAction 来处理请求，简单起见，使用注解方式配置</p>
<pre class=" language-bash"><code class="language-bash">@ParentPackage<span class="token punctuation">(</span><span class="token string">"struts-default"</span><span class="token punctuation">)</span>
public class UserAction extends ActionSupport implements ModelDriven<span class="token operator">&lt;</span>User<span class="token operator">></span><span class="token punctuation">{</span>
    private User user <span class="token operator">=</span> new User<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    @Action<span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"login"</span>, results <span class="token operator">=</span> <span class="token punctuation">{</span>
               @Result<span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">"success"</span>, location<span class="token operator">=</span><span class="token string">"/home.jsp"</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
    public String login<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception <span class="token punctuation">{</span>
        System.out.println<span class="token punctuation">(</span>user.getUsername<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
        System.out.println<span class="token punctuation">(</span>user.getPassword<span class="token punctuation">(</span><span class="token punctuation">))</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> SUCCESS<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    @Override
    public User getModel<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> user<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>实现 ModelDriven 方法，返回 user。<br>然后在执行 login 方法之前，ModelDriven 拦截器会给 user 设置请求提交的值。</p>
<h2 id="标签"><a href="#标签" class="headerlink" title="标签"></a>标签</h2><pre class=" language-bash"><code class="language-bash">tag1.jsp
<span class="token operator">&lt;</span>%@ page language<span class="token operator">=</span><span class="token string">"java"</span> contentType<span class="token operator">=</span><span class="token string">"text/html; charset=UTF-8"</span>
    pageEncoding<span class="token operator">=</span><span class="token string">"UTF-8"</span>%<span class="token operator">></span>
   <span class="token operator">&lt;</span>%@ taglib prefix<span class="token operator">=</span><span class="token string">"s"</span> uri<span class="token operator">=</span><span class="token string">"/struts-tags"</span> %<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>DOCTYPE html PUBLIC <span class="token string">"-//W3C//DTD HTML 4.01 Transitional//EN"</span> <span class="token string">"http://www.w3.org/TR/html4/loose.dtd"</span><span class="token operator">></span>
<span class="token operator">&lt;</span>html<span class="token operator">></span>
<span class="token operator">&lt;</span>head<span class="token operator">></span>
<span class="token operator">&lt;</span>meta http-equiv<span class="token operator">=</span><span class="token string">"Content-Type"</span> content<span class="token operator">=</span><span class="token string">"text/html; charset=UTF-8"</span><span class="token operator">></span>
<span class="token operator">&lt;</span>title<span class="token operator">></span>Insert title here<span class="token operator">&lt;</span>/title<span class="token operator">></span>
<span class="token operator">&lt;</span>/head<span class="token operator">></span>
<span class="token operator">&lt;</span>body<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>-- 遍历标签 iterator --<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>-- ------------------------------------- --<span class="token operator">></span>
<span class="token operator">&lt;</span>s:iterator value<span class="token operator">=</span><span class="token string">"#list"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span>s:property /<span class="token operator">></span><span class="token operator">&lt;</span>br<span class="token operator">></span>
<span class="token operator">&lt;</span>/s:iterator<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>-- ------------------------------------- --<span class="token operator">></span><span class="token operator">&lt;</span>hr<span class="token operator">></span>
<span class="token operator">&lt;</span>s:iterator value<span class="token operator">=</span><span class="token string">"#list"</span> var<span class="token operator">=</span><span class="token string">"name"</span> <span class="token operator">></span>
    <span class="token operator">&lt;</span>s:property value<span class="token operator">=</span><span class="token string">"#name"</span> /<span class="token operator">></span><span class="token operator">&lt;</span>br<span class="token operator">></span>
<span class="token operator">&lt;</span>/s:iterator<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>-- ------------------------------------- --<span class="token operator">></span><span class="token operator">&lt;</span>hr<span class="token operator">></span>
<span class="token operator">&lt;</span>s:iterator begin<span class="token operator">=</span><span class="token string">"1"</span> end<span class="token operator">=</span><span class="token string">"100"</span> step<span class="token operator">=</span><span class="token string">"1"</span>  <span class="token operator">></span>
    <span class="token operator">&lt;</span>s:property /<span class="token operator">></span><span class="token operator">|</span>
<span class="token operator">&lt;</span>/s:iterator<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>-- ------------------if <span class="token keyword">else</span> elseif------------------- --<span class="token operator">></span><span class="token operator">&lt;</span>hr<span class="token operator">></span>

<span class="token operator">&lt;</span>s:if test<span class="token operator">=</span><span class="token string">"#list.size()==4"</span><span class="token operator">></span>
    list长度为4<span class="token operator">!</span>
<span class="token operator">&lt;</span>/s:if<span class="token operator">></span>
<span class="token operator">&lt;</span>s:elseif test<span class="token operator">=</span><span class="token string">"#list.size()==3"</span><span class="token operator">></span>
    list长度为3<span class="token operator">!</span>
<span class="token operator">&lt;</span>/s:elseif<span class="token operator">></span>
<span class="token operator">&lt;</span>s:else<span class="token operator">></span>
    list不3不4<span class="token operator">!</span>
<span class="token operator">&lt;</span>/s:else<span class="token operator">></span>

<span class="token operator">&lt;</span><span class="token operator">!</span>-- ------------------property 配合ognl表达式页面取值 ------------------- --<span class="token operator">></span><span class="token operator">&lt;</span>hr<span class="token operator">></span>

<span class="token operator">&lt;</span>s:property value<span class="token operator">=</span><span class="token string">"#list.size()"</span> /<span class="token operator">></span>
<span class="token operator">&lt;</span>s:property value<span class="token operator">=</span><span class="token string">"#session.user.name"</span> /<span class="token operator">></span>

<span class="token operator">&lt;</span>/body<span class="token operator">></span>
<span class="token operator">&lt;</span>/html<span class="token operator">></span>
</code></pre>
<pre class=" language-bash"><code class="language-bash">package cn.itcast.b_tag<span class="token punctuation">;</span>

<span class="token function">import</span> java.util.ArrayList<span class="token punctuation">;</span>
<span class="token function">import</span> java.util.List<span class="token punctuation">;</span>

<span class="token function">import</span> com.opensymphony.xwork2.ActionContext<span class="token punctuation">;</span>
<span class="token function">import</span> com.opensymphony.xwork2.ActionSupport<span class="token punctuation">;</span>

public class Demo2Action extends ActionSupport <span class="token punctuation">{</span>


    public String execute<span class="token punctuation">(</span><span class="token punctuation">)</span> throws Exception <span class="token punctuation">{</span>

        List<span class="token operator">&lt;</span>String<span class="token operator">></span> list <span class="token operator">=</span> new ArrayList<span class="token operator">&lt;</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        list.add<span class="token punctuation">(</span><span class="token string">"tom"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        list.add<span class="token punctuation">(</span><span class="token string">"jerry"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        list.add<span class="token punctuation">(</span><span class="token string">"jack"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        list.add<span class="token punctuation">(</span><span class="token string">"rose"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        list.add<span class="token punctuation">(</span><span class="token string">"hqy"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        ActionContext.getContext<span class="token punctuation">(</span><span class="token punctuation">)</span>.put<span class="token punctuation">(</span><span class="token string">"list"</span>, list<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> SUCCESS<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>配置</p>
<pre class=" language-bash"><code class="language-bash"><span class="token operator">&lt;</span>?xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"UTF-8"</span>?<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">!</span>DOCTYPE struts PUBLIC
    <span class="token string">"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"</span>
    <span class="token string">"http://struts.apache.org/dtds/struts-2.3.dtd"</span><span class="token operator">></span>
<span class="token operator">&lt;</span>struts<span class="token operator">></span>
    <span class="token operator">&lt;</span>package name<span class="token operator">=</span><span class="token string">"tag"</span> namespace<span class="token operator">=</span><span class="token string">"/"</span> extends<span class="token operator">=</span><span class="token string">"struts-default"</span><span class="token operator">></span>
        <span class="token operator">&lt;</span>action name<span class="token operator">=</span><span class="token string">"Demo2Action"</span> class<span class="token operator">=</span><span class="token string">"cn.itcast.b_tag.Demo2Action"</span>
            method<span class="token operator">=</span><span class="token string">"execute"</span><span class="token operator">></span>
            <span class="token operator">&lt;</span>result name<span class="token operator">=</span><span class="token string">"success"</span> type<span class="token operator">=</span><span class="token string">"dispatcher"</span><span class="token operator">></span>/tag1.jsp<span class="token operator">&lt;</span>/result<span class="token operator">></span>
        <span class="token operator">&lt;</span>/action<span class="token operator">></span>
    <span class="token operator">&lt;</span>/package<span class="token operator">></span>
<span class="token operator">&lt;</span>/struts<span class="token operator">></span>
</code></pre>

            </div>
            <hr>

            
            <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.88rem;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff;
        background-color: #22AB38;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff;
        background-color: #019FE8;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a class="reward-link btn-floating btn-large waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close"><i class="fa fa-close"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs">
                        <li class="tab wechat-tab waves-effect waves-light"><a class="active" href="#wechat">微信</a></li>
                        <li class="tab alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                    </ul>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('#reward .reward-link').on('click', function () {
            $('#rewardModal').openModal();
        });

        $('#rewardModal .close').on('click', function () {
            $('#rewardModal').closeModal();
        });
    });
</script>
            

            <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    <div class="social-share" data-disabled="qzone" data-wechat-qrcode-helper="<p>微信里点“发现”->“扫一扫”二维码便可查看分享。</p>"></div>
    
</div>

<script src="/libs/share/js/social-share.min.js"></script>

            <div class="reprint">
                <p>
                    <span class="reprint-tip">转载请注明: </span>
                    <a href="https://zy20081.github.io" class="b-link-green">Zhou•Yong</a>
                    <i class="fa fa-angle-right fa-lg fa-fw text-color"></i>
                    <a href="/2018/11/25/struts2-zong-jie/" class="b-link-green">Struts2 总结</a>
                </p>
            </div>
        </div>
    </div>

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">上一篇</div>
            <div class="card">
                <a href="/2018/12/25/ssh-zheng-he-zhi-login/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/6.jpg" class="responsive-img" alt="SSH整合之Login">
                        
                        <span class="card-title">SSH整合之Login</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary">三大框架架构(整合原理)  
spring容器 &lt;?xml version="1.0" encoding="UTF-8"?>
&lt;beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-</div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="fa fa-clock-o fa-fw icon-date"></i>2018-12-25
                        </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/JAVA/" class="post-category" target="_blank">
                                    JAVA
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/框架/" target="_blank">
                        <span class="chip bg-color">框架</span>
                    </a>
                    
                    <a href="/tags/SSH/" target="_blank">
                        <span class="chip bg-color">SSH</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">下一篇</div>
            <div class="card">
                <a href="/2018/10/25/hibernate-zong-jie/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/18.jpg" class="responsive-img" alt="Hibernate 总结">
                        
                        <span class="card-title">Hibernate 总结</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary">Hibernate基本使用主配置1.导包，导入约束 2.主配置文件hibernate.cfg.xml
&lt;?xml version="1.0" encoding="UTF-8"?>
&lt;!DOCTYPE hibernate-conf</div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="fa fa-clock-o fa-fw icon-date"></i>2018-10-25
                            </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/JAVA/" class="post-category" target="_blank">
                                    JAVA
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/框架/" target="_blank">
                        <span class="chip bg-color">框架</span>
                    </a>
                    
                    <a href="/tags/SSH/" target="_blank">
                        <span class="chip bg-color">SSH</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>
</div>


    </div>
    <div class="col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title">目录</div>
            <div id="toc-content">

            </div>
        </div>
    </div>
</div>

<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });
    });
</script>
    

</main>


<footer class="page-footer bg-color">
    <div class="container row center-align">
        <div class="col s12 m8 l8 copy-right">
            本站由ZhouYong 技术分享，心得分享.
        </div>
        <div class="col s12 m4 l4 social-link"><a href="#!" class="tooltipped" data-tooltip="邮件联系我: zy20081@126.com" data-position="top" data-delay="50">
    <i class="fa fa-envelope-open"></i>
</a>
<a href="#!" class="tooltipped" data-tooltip="QQ联系我: 314153271" data-position="top" data-delay="50">
    <i class="fa fa-qq"></i>
</a>

<a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
    <i class="fa fa-rss"></i>
</a>
</div>
    </div>
</footer>

<div class="progress-bar"></div>


<!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title">搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字" class="search-input" autofocus>
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
</script>
<!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fa fa-angle-up"></i>
    </a>
</div>


<script src="/libs/materialize/js/materialize.min.js"></script>
<script src="/libs/masonry/masonry.pkgd.min.js"></script>
<script src="/libs/aos/aos.js"></script>
<script src="/libs/scrollprogress/scrollProgress.min.js"></script>
<script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>

<script src="/libs/others/clicklove.js"></script>

<script src="/js/matery.js"></script>
<!-- Global site tag (gtag.js) - Google Analytics -->


</body>
</html>